Generics <? super A> doesn't allow superTypes of A to be added to the list

Viewed 612

I am using wildcard and lower bound generics on list and yet compiler is throwing error.

Code:

int intStart = 0;       
Number num = new Integer(2);                
List<? super Integer> listOfNumbers = new ArrayList<>();
listOfNumbers.add(intStart);
listOfNumbers.add(num); //throws compiler error

Error :

The method add(capture#8-of ? super Integer) in the type List is not applicable for the arguments (Number)

With List<? super Integer>, I should have been allowed to add any objects of type Integer or its superTypes e.g. Number or Object. I've gone through some SO discussions, but couldn't find why I should be getting above error.

2 Answers
Related