• Post Reply Bookmark Topic Watch Topic
  • New Topic

How JVM knows to handle Runtime Polymorphism?  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How JVM knows to handle Runtime Polymorphism?
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a servlet question. Moved to Java in general.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand your question - I'm inclined to answer "because it was programmed to", but I guess that's not what you want to hear. So please elaborate...
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something called dynamic dispatcher in the JVM.
In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code (method) at runtime. This is done to support the cases where the appropriate method cannot be determined at compile-time (i.e. statically). Dynamic dispatch is only used for code invocation and not for other binding processes (such as for global variables) and the name is normally only used to describe a language feature where a runtime decision is required to determine which code to invoke.

Further java uses pointers to adopt dynamic dispatcher.
Each instance will have pointer to the methods and JVM maintains a table to map the objects and the methods
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure this is right ... somebody jump in if not ... restating roughly what Durgaprasad said ...

In pure dynamic, when you invoke a method on an object instance the runtime has to see if the class of that instance has the method. If not, the runtime has to walk up the inheritance stack and see if some ancestor class has the method. This is common in plenty of languages, very dynamic but relatively expensive. If you can keep all your classes in physical memory for minimal paging it's not too bad.

In Java the runtime goes to the class of the instance and the class has a direct connection to the code that will execute. That connection is built at compile time so there's no exploration of the inheritance stack. It's still runtime polymorphism because the compiler can't hook the caller up to the method because the compiler has no idea what the target object's class will be at runtime.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!