*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ***Confusing Generic question **** Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "***Confusing Generic question ****" Watch "***Confusing Generic question ****" New topic
Author

***Confusing Generic question ****

wonkty wang
Greenhorn

Joined: Jul 17, 2007
Posts: 29
Hi guys,

This is a generic question from k&B book. (p634)

Given a method declared as


A programmer want 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.

I could understand most of the choice except this one:

ArrayList<Integer> input = null;
ArrayList<Integer>output = null;

So why this is a incorrect answer? The explaination to this choice is that the return type of process is definitely declared as a List not an ArrayList.

But I thought ArrayList is the subtype of List, so it is equal to say

List<Integer> l = new ArrayList<Integer>();

so what was wrong with it? Can anyone please help?

Thanks.


SCJP 5.0 <br />-> Change job<br />-> Decide another cert to do
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi wonkty!
As you said,ArrayList is the subtype of List, so it is equal to say
List<Integer> l = new ArrayList<Integer>();
but is the below correct?
List<Integer> l; ArrayList<Integer> a=l;
Pay more attention to the method return type:List<E>
so if the output is :ArrayList<Integer>output = null;
it will get utput=List<Integer>.
dose it correct?


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
wonkty wang
Greenhorn

Joined: Jul 17, 2007
Posts: 29
Oh Shit. I must did too many of the questions lately.....How can I overlook it.....
 
Don't get me started about those stupid light bulbs.
 
subject: ***Confusing Generic question ****
 
Similar Threads
Sierra & Bates Self Test Question - Generics
Generics Question
SCJP 5 K&B question problem
Study Gude Chap 7 Q16 (errata applied)
Generics Question from K&B Study Guide.