X z = new Y();
means that at compile time it will treat z
as an instance of class X
, but at runtime as an instance of class Y
.
So why is: z.method((byte)0 + (char)0);
dealt with at runtime? Aren't these just 2 constants being added up, so it can be determined at compile time, instead of runtime?
class X {
void method(int x) { System.out.println("X:int"); }
}
class Y extends X {
void method(int x) { System.out.println("Y:int"); }
}
public class Z {
public static void main(String[] args) {
X x = new X();
X z = new Y();
System.out.println("1:");
z.method((byte)0 + (char)0);
}
}
Output:
1:
Y:int