Forums Register Login

Doubt in Generics

+Pie Number of slices to send: Send
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???
+Pie Number of slices to send: Send
I just saw the question on page 16 and the method is:

public static <E extends Number> List<E> process(List<E> nums)

and not
public static <E extends Number> List<? super E> process(List<E> nums)


Where did you get this one from.

For the first one C will be incorrect since the return type is List<Integer> which can't be applied to a List<Number>
+Pie Number of slices to send: Send
Hi Lalitha,

you are right this puzzled me as well when i was preparing. I am pretty sure this question is framed WRONG! But do try the things which compile successfully for various possibilities of input & output And please post them here!
+Pie Number of slices to send: Send
Hi,

Thanks for your reply..I have found this material in the cd which came along with K&B book.So if you substitute <List E> instead of <List ? super E> the correct answers will be B and F.
+Pie Number of slices to send: Send
Correct but suppose we have our return type as List<? super E> itself ! Then it creates a hell of a confusion!! For example look at this:

---It WORKS!
But NOT the following:

---this DOESNT work
there are other many possibilities which look like they should work but they dont!! "jo" can be returned as only some "super of Integer" like Number, but not Integer! And you have to define "ouput" as some <? super> or else it wont work!!If you have some explanation, please let me know
+Pie Number of slices to send: Send
Hi Nadeem,

Even I am also confused with this.I dont know why the compliler is giving error for this.
+Pie Number of slices to send: Send
I guess we should direct our query directly to K & B then
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1063 times.
Similar Threads
doubt abt Generics mock question - from K&B
Generics Question
wildcards in generics
doubt in generics........
Doubt in Generics
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:24:06.