I got a little bit confused the other day when it comes to generics and covariance/contravariance. I know C# specifies covariant/contravariant type parameters, but is there really a similar concept in Java?
I understand you can declare:
List<? extends String> l = ...
List<? super String> l2 =...
and now l can take list of MyString
, add method is not allowed etc. while l2 can take List<Object>
and add String to it, but cannot get any element, as described here Covariance- contravariance in Java. This, however, is simply declaring a List with a type parameter that accepts subtyes/supertypes, not doing "proper" covariance like:
Iterable<Animal> a = new Iterable<Dog>();
Am I confused or does Java simply not support proper covariance definitions? Thanks.