[Solved] Subtypes of Arrays


The JLS states that if B is assignable to A, then yes, B[] is assignable to A[].

This opens the door to serious implications though, demonstrated by this code:

class A {}
class B extends A {}
class C extends A {}

//...
B[] bs = new B[2];
A[] as = bs;
as[0] = new C(); //runtime error
B b = bs[0];

This code compiles but fails at runtime. We call that a lack of type safety.

solved Subtypes of Arrays