I have something like:
List<? extends BaseClass> a = getMyList();
Any of the following two instructions are invalid, and the compiler says that it requires a “? extends BaseClass” as an argument.
a.add(new BaseClass());
a.add(new SubClass());
I guess the problem is that
How can this be solved?
The problem is that the compiler has no way of determining the actual type of the list.
It could be that the type is
SubClass, in which case you would not be allowed to add aBaseClass.It could even be
SubSubClass, meaning you couldn’t add eitherBaseClassorSubClass.However, this will compile: