Consider this simple code:
class A {}
class B extends A {}
public class TestClass {
public static void main(String args[]) {
A[] a, a1;
B[] b;
a = new A[10];
a1 = a;
b = new B[20];
a = b;
b = (B[]) a;
b = (B[]) a1;
}
}
Look carefully at the lines that I commented on 1,2 and 3. Line 1 will be resolved at compile time, since assignment is from a subclass link to a superclass link.
Casting to line 2 is necessary because the superclass reference is applied to the subclass reference variable. And this works at runtime because the object referenced by a is actually an array from B (line 1).
Now, here's where my confusion lies: line 3 will throw java.lang.ClassCastException. Now this means that at runtime, the program understands that the actual object is not an array of B, but an array of A.
, . B A? , B IS-A A, ? , 3 - ?