• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Return type question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Studying for this exam, I have a return type question. I don't understand why Fig 1 is illegal but Fig 2 is legal, any hints? thanks

Fig 1:

public class Foo{
void go(){ }
}

public class Bar extends Foo {
String go () { //Illegal code
return null;
}

}

------
Fig 2:

class Alpha {
Alpha doStuff(char c) {
return new Alpha();
}
}

class Beta extends Alpha {
Beta doStuff(char c) { // legal override in Java 1.5
return new Beta();
}
}

 
Bartender
Posts: 2119
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Bar overrides go in Foo. When a method overrides another method, overriding method's return type should be the same as or subtype of that of the overriden method.


Yes, since JDK 1.5, overriding method can have the return type , which is a subtype of the return type in the overriden method.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic