Reputation: 4732
when dealing with wildcards such as setting/adding a generic item to a certain container is it suggested to use something like this?
void add(List<? super T> someList,someitem){
someList.add(someItem);
}
and when retrieving an item it is suggested to use something like this
<T> void f1(List<? extends T> obj, T item) {
obj.add(item);
}
What is the principle behind this? and when will I know if I should use this ?
Upvotes: 1
Views: 1171
Reputation: 424
you should have a look at the explanation of PECS principle
What is PECS (Producer Extends Consumer Super)?
In short, when you want to get information from an object, make sure to use extends with the wild card.
And when you want to put information into an object, make sure to use super along with wild card
Upvotes: 2