Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Covariant returns 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 returns" Watch "Covariant returns" New topic

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.

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.

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