wood burning stoves 2.0*
The moose likes Java in General and the fly likes How java Handles runtime Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How java Handles runtime Polymorphism" Watch "How java Handles runtime Polymorphism" New topic
Author

How java Handles runtime Polymorphism

Nancy Antony
Ranch Hand

Joined: Sep 06, 2007
Posts: 145

Hey Ranchers,

C++ handles runtime polymorphism with virtual pointers and virtual tables. How does Java do it? I know the process, no issues I'd like to know its internals. Any idea? Any website? Anyhting...

Thanks
nancy
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
To the extent I understand it, a java object at runtime has a jump table for its methods; which method actually gets called depends on the inheritance hieararchy. You define your own class A to extend Object, and then B to extend A; any object of any of the three classes has an entry in its jump table for, say, toString(). If you've defined toString in A, then objects of type A have a pointer to toString() in A's definition; if you do not define toString in B, then the jump table for an object of type B ALSO has a pointer to toString() in A's definition, and so forth.

When I looked at this some years ago, one difference between java and C++ was that C++ had a separate table for non-virtual methods. Java doesn't have non-virtual methods; any public method can be overridden (well, except for final, which is a compile-time animal. Let's leave final out of this).

Anyway. I don't know if that's what you were looking for, but more detail really should come from longer articles than the forum is liable to support, at least as a reply...

rc
Nancy Antony
Ranch Hand

Joined: Sep 06, 2007
Posts: 145

Thanks Ralph. I'm certainly looking at much more than this.that is how internal implementation of method call happens. I understand that all non-final methods by default are like virtual methods in Java. I wanted to see how parentRef=ChildObj denies to call a pure child method, the real tracing by JVM to implement method invocation.

Regards,
Nancy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How java Handles runtime Polymorphism