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; // 1
b = (B[]) a; // 2
b = (B[]) a1; // 3
}
}
Look closely at lines which I commented 1,2 and 3. The line 1 will be allowed during compilation, since assignment is done from a subclass reference to a superclass reference.
The cast in line 2 is needed because a superclass reference is assigned to a subclass reference variable. And this works at runtime because the object referenced to by a is actually of an array of B (line 1).
Now, here's where lies my confusion: line 3 will throw a java.lang.ClassCastException. Now, this means that during runtime, the program realizes that the actual object is not an array of B but is an array of A.
This is exactly what I don't understand. Doesn't B extends A? So it satisfies the condition B IS-A A, correct? And therefore, shouldn't line 3 not throw any exception during run time?
a1 = a; a = b; // 1- by changing the referenceayou are not changinga1. It still is pointing to the sameA[].