How come, in Java, I can write
List> list = new LinkedList();
but not
List>
Consider what would happen if you could.
List> list1 = new LinkedList>();
List> list2 = list1; // this shouldn't be allowed, because of the below
Container foo = new Container();
foo.add("hi");
list2.add(foo); // legal, because Container is a subtype of Container>
Container bar = list1.get(0);
Double x = bar.get(0); // type error; it is actually a String object
// this indicates that type safety was violated
However, using List extends Container>>
doesn't have this problem because you cannot put anything (except null
) into a list of this type (because there is no type that is guaranteed to be a subtype of "? extends Container>
").