This is because nums is a List<? extends Number>
, so the compiler knows that it is a List of Number or some subclass of Number, but it does not know which. Therefore, you will never be allowed to add anything to such a list. Here's an example of what this means:
List<? extends Number> nums= new ArrayList<Integer>();
and
List<? extends Number> nums= new ArrayList<Double>();
are both valid assignments. However, if you do:
nums.add(new Integer(4));
the compiler will not accept this as it cannot be certain that nums is a List of Integer
.