• Post Reply Bookmark Topic Watch Topic
  • New Topic

dynamic polymorphism in java  RSS feed

 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Can you please explain how the jvm handles dynamic ploymorphism in javs as in c++ there are virtual functions so the overwridden method basically the address and child class method refrence is maintained in the heap so at run time it can be identified so bascially how java handles about the overridden menthods please give with example..

Thanks,
Ujwwala
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In Java, all non-static methods, that are not private, behaves like "virtual functions" in C++. In fact, there is no way to turn off the behavior like C++.

Henry
 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but basically how jvm finds those methods in child class at runtimes?

 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean how does it maintains references in the heap in form of hash like?please ellaborate as someone asked me in the interview.

Thanks,
ujwwala
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujwwala tem wrote:but basically how jvm finds those methods in child class at runtimes?



Does it really matter? Its an implementation detail, and may vary from JVM to JVM. Regardless... I did have the pleasure, many years ago, of having this exact discussion with a JVM engineer involved with this aspect...


For the Sun JVM, it uses a jump table. For non-polymorphic methods, the addresses are resolved at compile time, and the code is generated to simply call the method. For polymorphic methods, the address of the jump table is resolved at compile time. And the code is generated to simply load the actual address from the jump table, and do an indirect call to the method.

Of course, due to the nature of java, the actual elements of the jump table must be filled out at runtime, after the object has been instantiated.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ujwwala tem wrote:I mean how does it maintains references in the heap in form of hash like?


Unfortunately, you need to explain what you mean by "references in the heap in the form of hash"?

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:For the Sun JVM, it uses a jump table. For non-polymorphic methods, the addresses are resolved at compile time, and the code is generated to simply call the method. For polymorphic methods, the address of the jump table is resolved at compile time. And the code is generated to simply load the actual address from the jump table, and do an indirect call to the method.

Of course, due to the nature of java, the actual elements of the jump table must be filled out at runtime, after the object has been instantiated.

That's the same as how it works in C++. In C++, the table is usually called the vtable. See also: Virtual method table (Wikipedia).
 
ujwwala tem
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the info..

Ujwwala
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!