Generic method invocation Clarification required
Hi,
I have a doubt regarding the generic method invocation described below.
This is a generic method declaration :
public static <E extends Number> List<? super E> process(List<E> nums){
return nums;
}
Now if i call the above method from the main method:
such as
ArrayList<Integer> n = null;
List<Number> numso= process(n);
The second line above in red gives a compilation error. Why does the "List<Number> numso" gives a compile error, since the return type declared is
"List<? super E>". If E is Integer then Super Of E can be Number and why List<Number> reference is not taking up the return type "List<? super E>" where E is an Integer ?
A bit unclear in generic return type of a method
But the return type declared in the method is List<? super E>. If E is an Integer, then super of Integer can be a Number i.e at runtime the return type will be List<? super Integer>. So why cant List<Number> reference variable can accept the return type of List<? super Integer> from the method "public static <E extends Number> List<? super E> process(List<E> nums)" ?
A bit more clarification required
Is it that during runtime/compiletime the method "public static <E extends Number> List<? super E> process(List<E> nums)" evaluates to "List<Integer>" instead of "List<? super Integer>" , when invoked like as
below:
ArrayList<Integer> n = null;
process(n);.
How does this happen can anyone please explain this ? Bit confused here.