File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Java in General
Bookmark "How java Handles runtime Polymorphism" Watch "How java Handles runtime Polymorphism" New topic

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...

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...

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.

I agree. Here's the link:
subject: How java Handles runtime Polymorphism
It's not a secret anymore!