File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Covariant Return Types" Watch "Covariant Return Types" New topic

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....

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...

I agree. Here's the link:
subject: Covariant Return Types
It's not a secret anymore!