运行时多态性是否总是在方法覆盖时发生,或者仅在方法覆盖期间将子类对象分配给超类变量后调用方法时才会发生?
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?
}
}