Lower-bounded generic collections (Collection<? super A>
) are writable - that's correct.
First, let's examine what type of collection we can assign to it.
List<? super Manager>
means that you can assign to this list a list of managers, a list of employees, a list of objects. All these assignments are perfectly valid and all of them will compile:
List<? super Manager> list;
list = new ArrayList<Manager>();
list = new ArrayList<Employee>();
list = new ArrayList<Object>();
That means that under the hood of list
can appear any of these lists. We know for sure only that could be a list of type Manager
or its super-types. And if it will be an ArrayList<Manager>
you can't put an Employee
object into this collection.
We can put an instance of Manager
into the Collections<Employee>
but not the other way around.
Let's consider another example:
ArrayList<? super String> list1 = new ArrayList<>();
list1.add((CharSequence) "Java"); // that will not compile
ArrayList<? super CharSequence> list2 = new ArrayList<>();
list2.add("Java");
Adding an object of type CharSequence
to the ArrayList<? super String>
will fail to compile because the interface CharSequence
is a super-type of String
.
But we can add a string to ArrayList<? super CharSequence>
because String
is compatible with CharSequence
type.