aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Covariant Return Types Mock from www.cafe4java.com 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 Return Types Mock from www.cafe4java.com" Watch "Covariant Return Types Mock from www.cafe4java.com" New topic
Author

Covariant Return Types Mock from www.cafe4java.com

Kram Nart
Ranch Hand

Joined: Jun 05, 2006
Posts: 32
class SuperCafe4Java {
public Object get (Object o) {
return ("SuperCafe4Java");
}
}

class SubCafe4Java extends SuperCafe4Java {
public Object get (String o) {
return ("SubCafe4Java");
}
}

class TestCafe4Java {
public static void main (String[] arguments) {
SuperCafe4Java superFoo;
SubCafe4Java subFoo;

superFoo = new SubCafe4Java();
System.out.println (superFoo.get("super"));

subFoo = new SubCafe4Java();
superFoo = subFoo;
System.out.println (superFoo.get("super"));
}
}

Can someone explain why "SuperCafe4Java" gets printed twice and not "SubCafe4Java"? I understand that the GET method is overloaded and not overriden here, but it still doesn't explain why "SuperCafe4Java" gets printed rather than "SubCafe4Java".


SCJP 5 <br /> SCWCD 5 <br /> SCBCD <br />
Figuring out what's next...
Shubha Kirani
Greenhorn

Joined: Jun 13, 2007
Posts: 12
In case of overriding, always check object type
and in case of overloading, always check reference type
[ June 17, 2007: Message edited by: Shubha Kirani ]
chintan ramavat
Ranch Hand

Joined: Sep 15, 2006
Posts: 134
Hello Ranchers

i think second scenario was obvious , in the first case you are calling the inherited method on SUB-Class reference with the SUPERCLASS Reference, though it's not displayed in the program but actually it's running meaning it's calling the inherited method of the SUB-Class.

- thank you
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Shubha Kirani:
In case of overriding, always check object type
and in case of overloading, always check reference type


It is making no sense to me.

It would check the visibility of the method through the object type and invoke the method through the object reference. Get back, if not clear.

However, you can check it by running the program after removing the method from parent and then from child. It wouldn't compile even, and you need make some amendments in your main method. Check out the compiler complains, play with it a little.
swapnil dangore
Ranch Hand

Joined: Jun 05, 2006
Posts: 46
Hi Kram,
You are right ! They are overloaded methods....
Overloading is compile time polymorphism....So compiler will place the call at compile time only....
Method are called on object refference only(exception is overridind)....So both times you are calling methods on SuperCafe4Java superFoo refference....So both the times SuperCafe4Java 's get() version will be called

Regards
Swapnil


Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
Kram Nart
Ranch Hand

Joined: Jun 05, 2006
Posts: 32
It's clearer now that I've had a chance to play with the code a little bit. The superFoo reference doesn't have access to any of SubCafe4Java's methods at all, including the "public Object get(String o)" at compile time. Polymorphism only kicks in when the methods are overridden.

Thanks everyone.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Covariant Return Types Mock from www.cafe4java.com