Can anyone explain how this both can compile and how it works?
List<? super String> list = new ArrayList<Object>();
As I understood it, the implementation of this needs to be either a String list or a list of objects that have String as super class? Have I missed something?
No (i.e. yes, you have missed something :-) . <? super String> is any class which is a superclass of String (including String itself). (In this case, the only other suitable class is Object.)
What you described would be <? extends String> (which in this specific case wouldn't be very useful as String is final, so it can have no subclasses).
<? super String> accepts String and any superclass.
Not to be confused with:
<? extends String> accepts String and any subclass (of which there aren't any since String is final).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With