aspose file tools
The moose likes Java in General and the fly likes dynamic polymorphism in java Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "dynamic polymorphism in java" Watch "dynamic polymorphism in java" New topic
Author

dynamic polymorphism in java

ujwwala tem
Ranch Hand

Joined: Feb 10, 2010
Posts: 70
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

Joined: Sep 28, 2004
Posts: 16810
    
  19


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
ujwwala tem
Ranch Hand

Joined: Feb 10, 2010
Posts: 70
but basically how jvm finds those methods in child class at runtimes?

ujwwala tem
Ranch Hand

Joined: Feb 10, 2010
Posts: 70
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

Joined: Sep 28, 2004
Posts: 16810
    
  19

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

Joined: Sep 28, 2004
Posts: 16810
    
  19

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
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ujwwala tem
Ranch Hand

Joined: Feb 10, 2010
Posts: 70
Hi,

Thanks for the info..

Ujwwala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic polymorphism in java
 
Similar Threads
Help on making my first servlet?
Application Server and Web Server
creating tooltips?
JasperReport : Dynamic SQL Query to inside <queryString> Tag of jrxml file
what is Echo ?