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".
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.
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.
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
Cheers<br />-------------<br />Swapnil<br /> <br />SCJP5-81%<br /> <br />"Dictionary is the only place where Success come before Work"
Joined: Jun 05, 2006
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.