I was reading why array covariance in Java is bad (Why are arrays covariant but generics are invariant?). If a Dog
is a subtype of Animal
, then a Dog[]
is a subtype of Animal[]
. This is a problem because things like this can be done:
Animal[] animals = new Dog[1];
animals[0] = new Cat();
This is different from generics which were implemented 'correctly'. A List<Dog>
is not a subtype of List<Animal>
I was trying to understand the essence of why it is bad and had just read about LSP. Did it violate the LSP in any way? There doesn't seem to be a clear violation.