This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Covariant Return Types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Covariant Return Types" Watch "Covariant Return Types" New topic
Author

Covariant Return Types

Jerry Ragland
Ranch Hand

Joined: Apr 26, 2005
Posts: 33
class Programmer {
Programmer debug() {return this;}
}

class SCJP extends Programmer {
Object debug() {return this;}
}

why the compilation fails saying in compatible return type during an override?
All class extends Object so SCJP IS-A Object and hence return this; should be accepted as a covariant return type ... Correct???

Some one please explain....

Thanks,
Jerry Ragland.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Object is a supertype of Programmer and not a subtype

Covariant Return types only applies to the subtypes of the overriding return type. In this case Programmer and al subclasses of Programmer.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Jerry Ragland
Ranch Hand

Joined: Apr 26, 2005
Posts: 33
Thanks for the clarification.. Got it...

-Jerry.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Covariant Return Types