posted 16 years ago
Hi,
I have a doubt in K&B chapter7 16th question .
Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow
the code to compile? (Choose all that apply.)
A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;
B. ArrayList<Integer> input = null;
List<Integer> output = null;
C. ArrayList<Integer> input = null;
List<Number> output = null;
D. List<Number> input = null;
ArrayList<Integer> output = null;
E. List<Number> input = null;
List<Number> output = null;
F. List<Integer> input = null;
List<Integer> output = null;
G. None of the above.
For the above question B,E,F are given as correct.I tried to compile the code with B as option.But I am getting compiler error as Type mismatch:cannot convert from List<capture-of ? super Integer> to List<Integer>.
Is this question framed wrong??I have another doubt in this question even the option C should also be correct since the return type can be List<Integer> or anthinf which is super of Integer .So Number should also be correct..
I have one more doubt in this question
Do the co-variant returns not applicable to generics??Like can't we return an ArrayList to a method whose return type was declared as List.IF this is not true why we are passing an argument of type ArrayList to a method whose parameter was declared as of type List???
Can anyone please help me in this regard???