• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
 
Get me the mayor's office! I need to tell him about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic