Originally posted by Mike Anakin: List<? extends String>
Says: This will take a list of Strings, or lists of or something that "is a" String (i.e. a subclass - as String is not an interface). Thus you can retrieve Strings from this list and be confident that they have all "String" behaviour (what you retrieve may be a String or any object lower in the inheritance hierarchy).
However, I (the compiler) will not let you add anything to this list, because you are not allowed to mix generic classes and I (the compiler) can't be sure that you weren't passed a "List of sub-class to String" and put a String in it.
So, you are accepting List<String>, or a List<Subclass of String> and accessing items knowing that they have String behaviour.
List<? super String>
Says: I'm expecting a list of String or anything higher up the inheritance tree (e.g. an Object). Now the compiler can be sure that if you add a "String" to the list, any list accessors will not be confused (as the objects retrieved will have the necessary behaviour.
So, you are accepting a List<Object>, or a List<String>. In either case a "String" can be added to the list. As when something else accesses the lists items (e.g. Objects from the List<Object> ) what is in the list definitely has the "Object" behaviour.
[ August 03, 2007: Message edited by: S J Martin ] [ August 03, 2007: Message edited by: S J Martin ]
Never attribute to malice that which is easily explained by incompetence.<br />SCJP (1.5)
I suggest huckleberry pie. But the only thing on the gluten free menu is this tiny ad: