Here I have this question. Let's say code 1 is Java with runtime polymorphism and code 2 is Java without runtime polymorphism.
Code 1:
class A {
void run() { System.out.println("A is running"); }
}
class B extends A{
void run(){ System.out.println("B is running with A!");}
public static void main(String args[]){
A a = new B(); //referenced to class A
a.run();
}
}
Code 2:
class A {
void run(){System.out.println("A is running");}
}
class B extends A{
void run(){ System.out.println("B is running with A!");}
public static void main(String args[]){
B a = new B(); //referenced to the same constructor class
a.run();
}
}
Even though these two codes give the exact same results, it is well known that runtime polymorphism is really important in OOP. I need an explanation/reason of using code 1 instead of using code 2.