I have read somewhere that runtime polymorphism is a result of dynamic typing in langugages. By inspecting the code below we can see a clear example of runtime polymorphism.
class A{
do(){}
}
class B extends A{
do(){}
}
...
A ex = new B();
ex.do();
Since there is superclass type reference , compiler can't decide which type will reference refer actually and binds method in the runtime.but what about the usage below with same class definitions?
My 1st question is for the example below;
class A{
do(){}
}
class B extends A{
//no overriding
}
...
A ex = new B();
ex.do();
There is only one version of method do() in the hierarchy. Does the system still wait runtime to bind the method?Or does it bind in compile time?
My second question is for the example below;
class A{
do(){}
}
class B extends A{
do(){}
}
...
B ex = new B();
ex.do();
There is subclass(lowest in inheritance chain) type reference now. Will it be bound in runtime?
Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In this process, an overridden method is called through the reference variable of a superclass.
Java For TestersMethod overriding is an example of runtime polymorphism. In method overriding, a subclass overrides a method with the same signature as that of in its superclass. During compile time, the check is made on the reference type.
The main advantage of Runtime Polymorphism is the ability of the class to offer the specification of its own to another inherited method. This transfer of implementation of one method to another method is possible without changing or modifying the codes of the parent class object.
The runtime polymorphism can be achieved by method overriding. Java virtual machine determines the proper method to call at the runtime, not at the compile time. It is also called dynamic or late binding. Method overriding says the child class has the same method as declared in the parent class.
When compiling a invocation of a non-static method javac
will always use a invokevirtual
instruction, so there is no optimization done at compile time.
But since devirtualization of method calls is an important optimization (save the vtable lookup, possibly inline the method) the runtime (hotspot, etc.) will try to apply it if possible due to code analysis.
So in your second example (third code block) the runtime might recognize that it can replace the virtual call to A.do
with a call to B.do
since ex
is actually a B
(should be trivial to figure out by the runtime in this case).
For your first example (second code block) there is another optimization technique. The runtime first sees class A
. Any invocations of A.do
are now compiled as static invocations as if no derived class exists which overrides A.do
. If such a class is later loaded the runtime will rollback this optimistic assumption and introduce virtual method calls instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With