List<? extends MyType>

Consider:

class MySubType extends MyType {
}

List<MySubType> subtypeList = new ArrayList<MySubType>();
List<? extends MyType> list = subtypeList;
list.add(new MyType());
MySubType sub = subtypeList.get(0);

sub now contains a MyType which is very wrong.


You shouldn't need to use the wildcard capture syntax in your case, simply declaring

List<MyType> listOfMytype;

should be enough. If you want to know exactly why, the Java Generics Tutorial has more than you would ever want to know about the esoteric craziness of Java Generics. Page 20 addresses your specific case.

As for why add with the wildcard capture does not work, it is because the compiler can't determine exactly what subclass of MyType the list will be in every case, so the compiler emits an error.


You cannot do a "put" with extends . Look at Generics - Get and Put rule.