Can someone tell me what this answer is? I think its C but thats not what the answer is.
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.
Just curious to know what is the answer. Is it "G. None of the above."? Also what makes you think that C should be the answer? Also could you please quote your source?
Thanks and Regards
Mike Rudiger
Greenhorn
Joined: Jan 06, 2008
Posts: 2
posted
0
the answer is b, e, and f and it also says this:
"The return type of process is definitely declared as a List, not an ArrayList, so A and D are wrong. C is wrong because the return type evaluates to List<Integer>, and that can't be assigned to a variable of type List<Number>. Of course all these would probably cause a NullPointerException since the variables are still null�but the question only asked us to get the code to compile."
It is from the Cathy Sierra
Satya Maheshwari
Ranch Hand
Joined: Jan 01, 2007
Posts: 368
posted
0
I think the answer for this should be "None of the above" unless something is seriously wrong with my logic. The compiler(1.5.11) agrees with the discussion below:
Method Argument is of type List<E> where <E extends Number>. Hence the method argument can be a List of objects which extend number. For e.g. List<Float>, List<Double>, List<Integer> are all valid.
Method Return Type is List<? super E> where <E extends Number>. Hence the method return type is a list of objects which are superclass of an object which itself extends number. Hence return type is undefined due to ambiguity(it may be Number or some superclass of Number or Object etc. etc.)
Now moving to the options:
A. ArrayList<Integer> input = null; ArrayList<Integer> output = null; This is obviously wrong as return type of process is List which can never be assigned to an ArrayList without casting.
B. ArrayList<Integer> input = null; List<Integer> output = null; This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Integer>
C. ArrayList<Integer> input = null; List<Number> output = null; This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Number>
D. List<Number> input = null; ArrayList<Integer> output = null; This is obviously wrong as return type of process is List which can never be assigned to an ArrayList without casting.
E. List<Number> input = null; List<Number> output = null; This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Number>. Also the method argument is also wrong as it can be a List of objects which extend number but here it is List<Number>.
F. List<Integer> input = null; List<Integer> output = null; This is wrong as the return type is ambiguous(See discussion above on Method Return Type). As it is ambiguous, it cannot be List<Integer>
G. None of the above. Correct!!
Please comment if this resoning is correct/incorrect.
Still I dont understand the output. return type List<E> so we can return only a List and E could be an Integer. But how can answer B be right. the method argument is a List and in option B we are sending ArrayList. Can we send subtype of a Collection as an argument??
I have exam tomorrow morning and I am banging my head with Generics.
If you worry you cannot work... If you work you need not worry
Satya Maheshwari
Ranch Hand
Joined: Jan 01, 2007
Posts: 368
posted
0
the method argument is a List and in option B we are sending ArrayList. Can we send subtype of a Collection as an argument??