File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Generics" Watch "Doubt in Generics" New topic
Author

Doubt in Generics

lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
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???
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126
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>
Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
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!


<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
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.
Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
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
lalitha kaparapu
Greenhorn

Joined: Nov 05, 2007
Posts: 16
Hi Nadeem,

Even I am also confused with this.I dont know why the compliler is giving error for this.
Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
I guess we should direct our query directly to K & B then
 
wood burning stoves
 
subject: Doubt in Generics
 
Similar Threads
Doubt in Generics
Generics Question
doubt in generics........
doubt abt Generics mock question - from K&B
wildcards in generics