Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How java Handles runtime Polymorphism

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic