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 returns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
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!