my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Polymorphism and overriding methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism and overriding methods" Watch "Polymorphism and overriding methods" New topic
Author

Polymorphism and overriding methods

C Law
Greenhorn

Joined: Mar 05, 2006
Posts: 21
Hello,

On pg. 104 of K & B, it reads:

**
If a method is overridden but you use a polymorphic (supertype)
reference to refer to the subtype object with the overriding method,
the compiler assumes you�re calling the supertype version of the method.
**

I understand that the subclass version of (overriding) method will
be executed at runtime (instance methods have runtime polymorphism).
But how come that the *compiler" thinks the superclass version will
be used? Why the difference in compile time and runtime?

Thanks!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I don't have their book in front of me to look at, so I can't try to 'splain what exactly they wuz thinkin' here, but as a general rule the compiler doesn't know, doesn't need to know, and don't pretend to know the runtime types of any objects that any variable might refer to. It knows the compile-time types of variables, return types, argument types, etc, and that's it.

So in the case described, the compiler will (just as a C++ compiler would) generate a call to the base class method. In C++, that call would likely be routed through a vtbl, and at runtime, would get resolved to the subclass method. In Java, the lookup mechanisms are different, but the end result is the same: the compiler tries to invoke the base class method, and ends up invoking the subclass method at runtime.

Got it, pilgrim?


[Jess in Action][AskingGoodQuestions]
C Law
Greenhorn

Joined: Mar 05, 2006
Posts: 21
Originally posted by Ernest Friedman-Hill:
I don't have their book in front of me to look at, so I can't try to 'splain what exactly they wuz thinkin' here, but as a general rule the compiler doesn't know, doesn't need to know, and don't pretend to know the runtime types of any objects that any variable might refer to. It knows the compile-time types of variables, return types, argument types, etc, and that's it.

So in the case described, the compiler will (just as a C++ compiler would) generate a call to the base class method. In C++, that call would likely be routed through a vtbl, and at runtime, would get resolved to the subclass method. In Java, the lookup mechanisms are different, but the end result is the same: the compiler tries to invoke the base class method, and ends up invoking the subclass method at runtime.

Got it, pilgrim?


I understand the first para of your post, but not the second, which
is too advanced for me.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You mentioned C before, I was just using C++ jargon in an attempt to make things clearer for you. Didn't work, did it?

If you understand polymorphism, then everything is great. I can't tell you what K&B are trying to say in this quote, but the impression you got (which is the same impression it gives me, I think) is wrong -- the compiler doesn't think about the various implementations of the method at all. The compiler just generates code to call the method as defined with respect to the variable's declared type; runtime polymorphism takes care of deciding which implementation to call.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism and overriding methods