Forums Register Login

How JVM knows to handle Runtime Polymorphism?

+Pie Number of slices to send: Send
How JVM knows to handle Runtime Polymorphism?
+Pie Number of slices to send: Send
Not a servlet question. Moved to Java in general.
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1753 times.
Similar Threads
Why isn't this generics based instantiation working?
How to load a Jar Library dynamically?
Is Polymorphism and Late Binding the same or different?
throw and throws
How JVM knows to handle Runtime Polymorphism?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:33:32.