PrepZone Logo
PrepZone
Back to Java

Java · Hard

What are covariance and contravariance in Java generics? Why is List<Dog> not a List<Animal>?

JavaCore Java

Answer preview

Covariance — If Dog extends Animal, then List&lt;Dog&gt; should be a subtype of List&lt;Animal&gt; — but in Java, it is NOT.…