| 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
|
 |
 |
|
|
subject: Covariant returns
|
|
|