aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Chapter 7 Question14 : wrong answer 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 "Chapter 7 Question14 : wrong answer " Watch "Chapter 7 Question14 : wrong answer " New topic
Author

Chapter 7 Question14 : wrong answer

tarik el berrak
Greenhorn

Joined: Oct 06, 2006
Posts: 29
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 ]

SCJP 1.5<br />New Target (SCWCD 5)
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Collection to List is a downcast, so the compiler won't accept it without an explicit cast. List to Collection can be done implicitly.


SCJP 5.0
tarik el berrak
Greenhorn

Joined: Oct 06, 2006
Posts: 29
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

Joined: Oct 20, 2007
Posts: 513
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

Joined: Oct 06, 2006
Posts: 29
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
 
Don't get me started about those stupid light bulbs.
 
subject: Chapter 7 Question14 : wrong answer