Does runtime polymorphism always happen with method overriding, or does it happen only if method is called after assigning sub class object to super class variable, during method overriding?
class A {
public void myFunc() {
System.out.println("Something");
}
}
class B extends A {
public void myFunc() {
System.out.println("Something else");
}
public static void main (String args[]) {
A obj = new B();
obj.myFunc(); //Is only this call resolved at run time?
A obj2 = new A();
obj2.myFunc(); //Or is this call too resolved at run time?
B obj3 = new B();
obj3.myFunc(); //Is this call resolved at compile time?
}
}
Method 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.
Answer to why method overriding is called runtime polymorphism in java is because the methods get resolved at the Run-Time. In simple words, when you execute a program, the method of which class out of many will be called, if they have overridden the method.
Method overriding uses the dynamic method dispatch technique to resolve the method call and decide whether to call a superclass or subclass method and this is done at runtime. Hence runtime polymorphism is also called dynamic polymorphism or late binding.
Method overriding is a run-time polymorphism. It helps to increase the readability of the program. It is used to grant the specific implementation of the method which is already provided by its parent class or superclass.
In method overriding, method resolution always takes care by JVM based on the RUN TIME OBJECT. Yes, all these calls will get resolved at run-time.
A obj = new B();
obj.myFunc();
A obj2 = new A();
obj2.myFunc();
B obj3 = new B();
obj3.myFunc();
Whether the compiler is actually able to optimize any or all of the three method invocations and resolve the calls at compile time now or in some future implementation is irrelevant: All three method invocations are examples of runtime polymorphism, also known as dynamic dispatch This is contrasted with what is referred to as static polymorphism, also known as method overloading, which is a class having multiple methods with the same name but with different argument types.
Here is a small Java program:
public class Test {
public void foo(String inputString)
{
System.out.println(inputString);
}
public static void main(String[] args) {
Test test = new Test();
test.foo("a"); # very obvious method invocation
}
}
It is very clear to a human what method would be invoked at runtime. Here is the disassembled bytecode:
Compiled from "Test.java"
public class Test {
public Test();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public void foo(java.lang.String);
Code:
0: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
3: aload_1
4: invokevirtual #3 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
7: return
public static void main(java.lang.String[]);
Code:
0: new #4 // class Test
3: dup
4: invokespecial #5 // Method "<init>":()V
7: astore_1
8: aload_1
9: ldc #6 // String a
11: invokevirtual #7 // Method foo:(Ljava/lang/String;)V
14: return
}
Instruction 11 is a runtime dispatch of a virtual method.
But as I said, even if in some future compiler implementation this was optimized to a different call, that would just be an implementation detail.
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