Generics interact with inheritance in a non-obvious way. The most important rule: if Dog extends Animal, List<Dog> is NOT a subtype of List<Animal>. This is called invariance and exists to preserve type safety.
Generic Types Are Invariant
Generics interact with inheritance in a non-obvious way. The most important rule: if Dog extends Animal, List<Dog> is NOT a subtype of List<Animal>. This is called invariance and exists to preserve type safety.