I'm struggling to understand what I consider a fundamental question in Java. Below the answer is given that line 3 leans to a class cast exception. I can see that originally a1 pointed to an array of object A. But on line 1 doesn't a get set to point to an array of object b? And therefore if a1 was pointing to a should it now not also point to b?
Explanation from Enthuware:-
The program will throw a java.lang.ClassCastException at the line labelled 3 when run.
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. Now, the cast at line //3 tells the compiler not to worry, that I'm a good programmer and I know what I am doing and the object referenced by the super class reference (a1) will actually be of class B at run time. So there is no compile time error. But at run time, this fails because the actual object is not an array of B but is an array of A.
public static void main(String args[]) {
A[] a, a1;
B[] b;
a = new A[10];
a1 = a;
b = new B[20];
a = b; //line 1
b = (B[]) a; //line 2
b = (B[]) a1; //line 3
}
}
class A {
}
class B extends A {}