• 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

List declartion

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I noticed that in the book, sometimes it is trying to do this:
List<Integer> iL = new ArrayList<Integer>();
instead of this:
ArrayList<Integer> iL = new ArrayList<Integer>();

Isn't List an interface class? can we do a new on that?
If we can do a new on it, is there any ArrayList specfic method that when we have to call we have to cast it from List to ArrayList?
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Men,

List is an interface, and you can never instantiate one.

However, if you look closer at this code, you will find that the instantiation is of an ArrayList and never of a List.
An ArrayList is-a List, so we are looking here at an example of polymorphic instantiation.

Cheers,
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Or with other words: iL is a reference variable of type List. Since ArrayList implements List, ArrayList is-a List. So iL can refer to ArrayList objects. List is the type of the reference iL, and instantiated is an ArrayList (you do a 'new' on ArrayList, not on List).
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys!
Does it mean that iL can only use the API in the list interface without a cast?
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, but that is a good thing. That is the benefit of using interfaces. The interface should define the API, the concrete classes implement methods defined by the common interface, that way a reference of the interface type can hold a reference to any of the concrete classes and they will all behave as the API says it should. By using List<ClassType> ref to declare a reference it can hold an ArrayList, a Vector, a LinkedList, OR any of your own classes that extend one of the list types.
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see, thank you.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic