I have read some article about generic covariant, and a learn a common knowledge about covariant and contravariant describe how the subtype relation is affected by type transformations. That is, if A and B are types, f is a type transformation, and ≤ the subtype relation (i.e. A ≤ B means that A is a subtype of B), we have
- f is covariant if A ≤ B implies that f(A) ≤ f(B)
- f is contravariant if A ≤ B implies that f(B) ≤ f(A)
- f is invariant if neither of the above holds
as we know, ? extends T
is covariant, so we can say:
because of ? extends Number ≤ Number
so List<? extends Number> ≤ List<Number>
, and this means List<? extends Number>
is a subtype of List<Number>
List<? extends Number> a1 = null;
List<Number> a2 = null;
a2 = a1; // compile error
a1 = a2; // compile success
so why a1 = a2
can be compiled, but a2 = a1
can't.
Don't List<? extends Number>
is a subtype of List<Number>
means a2 = a1
?
wait and hope your answer !