In Java, if Car
is a derived class of Vehicle
, then we can treat all Cars
as Vehicles
; a Car
is a Vehicle
. However, a List
of Cars
is not also a List
of Vehicles
. We say that List<Car>
is not covariant with List<Vehicle>
.
Java requires you to explicitly tell it when you would like to use covariance and contravariance with wildcards, represented by the ?
token. Take a look at where your problem happens:
List<List<? extends Number>> l = new ArrayList<List<Number>>();
// ---------------- ------
//
// "? extends Number" matched by "Number". Success!
The inner List<? extends Number>
works because Number
does indeed extend Number
, so it matches "? extends Number
". So far, so good. What's next?
List<List<? extends Number>> l = new ArrayList<List<Number>>();
// ---------------------- ------------
//
// "List<? extends Number>" not matched by "List<Number>". These are
// different types and covariance is not specified with a wildcard.
// Failure.
However, the combined inner type parameter List<? extends Number>
is not matched by List<Number>
; the types must be exactly identical. Another wildcard will tell Java that this combined type should also be covariant:
List<? extends List<? extends Number>> l = new ArrayList<List<Number>>();
> on the LHS would better match the poster's intent.
– erickson Apr 14 '09 at 15:29>` gives the compilation error `incompatible types: ArrayList
– skomisa Mar 11 '20 at 17:34> cannot be converted to List
>`. It's counter intuitive (to me), but you can't use List> on the LHS when `new ArrayList
>();` is on the RHS. Things get tricky with multi-level wildcards. See [What is the difference between extends Base> and?](https://stackoverflow.com/q/60536437/2985643) for a recent related question.