Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Covariant returns

 
C Kushtawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a simple program that I have written:



But the same program compiles fine when the return types are of Object types as below :




Why is it so?Please explain.
 
Vierda Mila
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kusthawar, the explanation is simple Cat is-a Animal so the covariant returns ok, but int is-a NOT long.
Hope it clear your doubt.
 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As vierda was talking about covariant returns, the return type of a method in subclass should be subclass of the return type of the method in the super class. Animal is a super class of cat so it works fine, but int is not a super class of long.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic