This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the same question?
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, even the "correct" options do not work.
 
Amit Batra
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Aniket. and wise for the link
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic