• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

***Confusing Generic question ****

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
wonkty wang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic