This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 7 Question14 : wrong answer

 
tarik el berrak
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I'm confused with the answer given in the K&S book for the question 14 of the chapter7 (Generics). here is the explanation given in the book :

(B is wrong because longWords has been declared as a Collection<E>, and that can't be implicitly converted to a List<E> to match the declared return value. (Even though we know that longWords is really an ArrayList<E>, the compiler only know what it's been declared as.
)

for me the answer B is correct and here's an example :

---------------------------Code-----------------------------------------------
public class Chapter7Question14 {

public static void main(String[] args) {

String[] array = { "123", "12345678", "1", "12", "1234567890"};
List<String> list = Arrays.asList(array);
Collection<String> resultList = getLongWords(list);
}

public static <E extends CharSequence> List<E> getLongWords(Collection<E> col){

return new ArrayList<E>();
}

}
---------------------------------------------------------------------------

I'm Right ?

[ November 24, 2007: Message edited by: tarik el berrak ]
[ November 24, 2007: Message edited by: tarik el berrak ]
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection to List is a downcast, so the compiler won't accept it without an explicit cast. List to Collection can be done implicitly.
 
tarik el berrak
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure,
but in this example, the getLongWords return a List<E> (E is a String in this case), so It can be affected to a Collection<E> polymorphically (implicit cast)

the piece of code compiles and run fine (correspond to anser B), so for me the B is correct. Can you explain more please ?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's where I think you're getting confused: the problem is not with the return value of getLongWords() getting assigned to resultList. Rather, within getLongWords(), the longWords local variable is declared as a Collection<E>, so it cannot be returned as a List<E> without an explicit cast. You'd need to change the return statement to "return (List<E> longWords;" to make that work.
 
tarik el berrak
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I forget the method body and focused on method signature. You are right, since the return is a collection<E>, it cannot be assigned to a List without explicit cast.
I have just question, is there a strategy to follow when answering the exam questions: for example, if within the answers proposed we find "Compilation fails", we have to look for errors first and then try to see what the code is suppposed to do if it compiles fine ??

Thanks in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic