• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism and overriding methods

 
C Law
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
C Law
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic