• 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

Is this Generics model answer correct

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Studying for SCJP5 using Sierra/Bates book.

In generics end of ch question 16 has the answer as below. However I am not sure its correct.

public static <E extends Number> List<? super E> process(List <E>

//if you want to invoke the method like this
ArrayList<Integer>input=null
List<Integer>output=null

output=process(input)//will this invocation work with the above 2 lines?

//I thought the process method requires a list as input.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[sam] I thought the process method requires a list as input.

It does. input "is a" List. (It's an instance of ArrayList which is a subclass of List.)
 
sam Asare
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So =would it work with:

ArrayList<Integer> input=null
ArrayList<Integer>output=null
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mmmmmm. No.

output should not be defined as an ArrayList. It should be a List.

The return type of the process() method is List so it could return a LinkedList, ArrayList or any other object that implements the List interface.

You can't store a reference to a LinkedList in an ArrayList reference.
 
sam Asare
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks- Your explanation has cleared my confusion
 
That feels good. Thanks. Here's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic