Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in K&B question-16 p621(Generics) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in K&B question-16 p621(Generics)" Watch "Doubt in K&B question-16 p621(Generics)" New topic
Author

Doubt in K&B question-16 p621(Generics)

Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
Hi,
"Given a method declared as:
public static<E extends Number>List<E>process(list<E>num)

a programmer wants to use this method like this:
//insert declaration here
output=process(input)"

My question is the correct answer is shown as the one where the return type is declared as List<Integer> type, but why cant we use any of the options that have an arraylist<integer> return type.? Arraylist is a subtype of list so why cant we do that?
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
In option (D), List<Number> is not assignable to ArrayList<Integer>, hence this option is not correct.

In option (A), the return type of the method would be List<Integer>. It would not be possible to assign a List<Integer> to an ArrayList<Integer>. This too, is incorrect.

Hope this answers your query.


SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
In option (A), the return type of the method would be List<Integer>. It would not be possible to assign a List<Integer> to an ArrayList<Integer>. This too, is incorrect.


This is incorrect, the return type of the method is shown as List<E>. and option A would be trying to assign an Arraylist to a list. My question was why couldnt an ArrayList be returned in a place where a list was expected. Option A seems a valid candidate?
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
This is the crux point. An ArrayList can sure be assigned to a List, but what will be returned is a List reference pointing to an ArrayList object.

The List reference would then be assigned to an ArrayList reference, which would not work out.

Consider this sample program:


Would this work out? No, coz although you are assigning an integer to the returnInteger method, the compiler does not have enough information to know that. Hence, you would end up returning an Integer as a Number (just as you would return an ArrayList as a List). In main(), you try to asssign a Number to an Integer which causes the error:

javac Demo.java
Demo.java:11: incompatible types
found : java.lang.Number
required: java.lang.Integer
Integer i = returnInteger(5);
^
1 error


You would need to cast the Number to an Integer (Similar cast from a List to an ArrayList is needed) for the code to work.
[ August 16, 2006: Message edited by: Aniket Patil ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
the same question?
Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218
Yes, even the "correct" options do not work.
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
Thanks Aniket. and wise for the link
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Doubt in K&B question-16 p621(Generics)