I was learning Java generics lately and came across the so-called "get-put" principle, i.e. which kind of wildcards allow you to add or remove certain types of objects from a collection (reference, e.g. https://flylib.com/books/en/4.79.1.18/1/).
My problem is, it is said that you cannot get anything but Object(s) from a collection that uses <? super SomeClass>
. But the following code is perfectly valid:
List<? super A> list = new ArrayList<>();
list.add(new A());
System.out.println((list.get(0).toString()));
where
class A{
@Override
public String toString(){
return "super.toString();";
}
}
The funny thing is, it really uses the overridden toString(), contrary to the principle.
Furthermore,
A a = list.get(0);
fails.
Could anyone explain what's the issue here?