• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Covariant Return Types

 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification.. Got it...

-Jerry.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic