The code below makes complete sense to me – its about adding an element of some type which is supertype of type T and type S is definitely such a super type , so why the compiler refuses to add ‘element’ into the collection ?
class GenericType<S,T extends S>{
void add1(Collection<? super T> col ,S element ){
col.add(element); // error
// The method add(capture#9-of ? super T) in the type
// Collection<capture#9-of ? super T> is not applicable for the arguments (S)
}
}
Collection<? super T>does not mean “a collection that can containTand any superclass of it” – it’s actually not possible to formulate that restriction. What it means is “a collection that can only contain instances of some specific class which is a superclass ofT” – basically it ensures that you can add aTto the collection.The method can be called with a
Collection<T>, yet you want to add anSto it.