This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Covariant returns Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Covariant returns" Watch "Covariant returns" New topic
Author

Covariant returns

C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
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

Joined: Feb 25, 2008
Posts: 61
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.


SCJP 5
Ravikanth kolli
Ranch Hand

Joined: Feb 10, 2008
Posts: 179

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.


-kolli
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Covariant returns
 
Similar Threads
Covariant Return
invalid cast exception
instanceof operator
Doubt Example
ClassCastException