Can someone please explain why we would ever use ? extends ...
. To me, it seems that I can always use the super type whenever I want to specify such a condition in my code. For example, ? extends Number
can always be replaced with Number
right?
Consider the following
public static double numberAdd(List<Number> list) {
double sum = 0;
for (Number x : list) {
sum += x.doubleValue();
}
return sum;
}
vs.
public static double genericAdd(List<? extends Number> list) {
double sum = 0;
for (Number x : list) {
sum += x.doubleValue();
}
return sum;
}
Both do the same thing^. Any reason why you would use the List<? extends Number>
version?