I have a question about compile-time type (static) and run-time type (dynamic) types. As far as I understood it if the subclass overrides some method of superclass then while calling something like Parent pTom = tom, the pTom's method that was overridden must not be used and instead the one that overrides it must be instantiated.
public class Parent {
int x = 0;
static void f() {
System.out.println("Ahem");
}
}
public class Child extends Parent {
int x = 5;
static void f() {
System.out.println("No way!");
}
public static void main(String[] args) {
Child tom = new Child();
System.out.println(tom.x); //output is No way!
Parent pTom = tom;
pTom.f();
System.out.println(pTom.x); //output is Ahem
}
}
Can you help me to clarify my understanding?