package test;
import java.util.ArrayList;
import java.util.List;
public class Test3 {
public static void main(String[] args) {
List<Number> number = new ArrayList<Number>();
number.add(new Integer(10));
number.add(new Double(10));
List<Integer> inte = new ArrayList<Integer>();
List<Number> numInt = new ArrayList<Integer>(); // compile error
sum(number);
sum(inte); // compile error
}
public static void sum(List<Number> n){
}
}
I understand that List<Number>
is not equal to List<Integer>
from the Java Docs. And that Object
is the parent class of both.
If you want to pass sub-types of Number, either do public static void sum(List<? extends Number> n)
or public static <T extends Number> void(List<T> n)
My Question:
List<Number>
can contain a mix of Number
Integer
Double
. Yet only the methods present in Number
is accessible inside sum
method. Even if I do List<? extends Number>
instead of List<Number>
, this is also giving me access only to methods of Number
.
So,What usefulness/purpose is provided by considering List<Number>
NOT equal to List<Integer>
? (That is, how this restriction is useful?) Why do we need a separate syntax List<? extends Number>
?