Think about your definition: ArrayList<? extends Type>
. It means list of elements each of them extends Type
. It does not mean that each of them is of type T
as itemToAdd
. To make this code to compile you have to ensure this fact, i.e. use T
in all relevant places:
public <T extends Type> void addItem(T itemToAdd, Class<T> classType) {
List<T> allItems = getAllItems(classType);
allItems.add(itemToAdd);
}
It probably means that you should change definition of getAllItem()
to
public <T extends Type> List<T> getAllItems(Class<T> classType)
BTW please pay attention that I changed your ArrayList
to List
. Although it is out of the topic but avoid using concrete classes at the left part of assignment operator, as a return value of method and as a method parameter.