Compile Time Rule :- When oldType and newType are classes, one class must be the subclass of another, no matter which one.. For this reason, the compilation succeeds in above code.
Run Time Rule :- When newType is a class, the class of the expression being converted must be newType or must inherit from newType.. Here the class of the expression being converted is A which is neither B nor inherits from B. Therefore the ClassCastException at run time.
Similarly, for Arrays :-
Compile Time Rule :- When both oldType and newType are arrays, both arrays must contain reference types (not primitives) and an element of one type must be same or subclass of the element of another type, no matter which one.
Run Time Rule :- The element of the type being converted (oldType..i.e., the actual object type it refers to) must be same as or subclass of the element of the newType otherwise ClassCastException