This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes covariant doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "covariant doubt" Watch "covariant doubt" New topic
Author

covariant doubt

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135

I guess answer would be
SubCafe4Java
SubCafe4Java
But when i run the programme it shows
SuperCafe4Java
SuperCafe4Java

We are calling
superFoo.get("super") i.e., superclass reference with subclass object in it, also parameter "super" is a String object. So it is more appropriate to call subclass get() but why superclass get() called?

I can't understand.... thanks in advance for any help...


live
hemadri raju
Greenhorn

Joined: Mar 28, 2006
Posts: 15
when you are redifining the function in the child class you are not overloading the base class function, because the base class is having parameter as Object but child class is having parameter as String. So it is not covariant return.

and when you are calling the funciton you are calling using the base class version. so always function of the base class is getting called
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Where are the covariant retrun types?
I think you got confused and pretends to think that the methods are overridded.Infact the methods are overloaded in first two classes.To make it a valid overrided method with covariant return type make change as


~Sanjeev Singh<br />SCJP 1.5
Bob CHOI
Ranch Hand

Joined: Nov 10, 2006
Posts: 127


2nd one can't override 1st because the argument signature is different even thought String is subclass of Object, hence it's called overloaded - name reuse but new method.



Now superFoo owns only the 1st method, it's always legal - here the argument Object is subcast to String again when get("super") being called.

finally it prints out "SuperCafe4Java";


Hard work rewards
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
thanks a lot to all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: covariant doubt
 
Similar Threads
Can Any Explain This Code...
method overriding
covariant data types and overloaded method
Covariant Return Types Mock from www.cafe4java.com
why this output?