In this example:
import java.util.*;
public class Example {
static void doesntCompile(Map> map) {}
static &l
Simplied example of demonstration. Same example can be visualize like below.
static void demo(List> lst) {} // doesn't work
static void demo(List extends Pair extends Number>> lst) {} // works
demo(new ArrayList()); // works
demo(new ArrayList()); // works for subtype too
public static class Pair {}
public static class SubPair extends Pair {}
List
is a multi-level wildcards type whereas List extends Number>
is a standard wildcard type .
Valid concrete instantiations of the wild card type List extends Number>
include Number
and any subtypes of Number
whereas in case of List
which is a type argument of type argument and itself has a concrete instantiation of the generic type.
Generics are invariant so Pair extends Number>
wild card type can only accept Pair extends Number>>
. Inner type ? extends Number
is already covariant. You have to make the enclosing type as covariant to allow covariance.