• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Using List interface

 
Ranch Hand
Posts: 87
IntelliJ IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Everyone,

In many places I have seen developers doing something like this with ArrayList:


If at all I am sure that I need ArrayList only then what is the need of having declared it as a reference of List type?

I understand that there is a coding practice followed as Coding To an Interface, but in above case is that really needed?

Please correct me if I am wrong.

Thanks i n advance.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's almost always a good idea to code to the interface, especially if the instance ends up being passed to other methods or returned as a method value. Except for when the instance is created, code should only care that it is a List -- the fact that it is an ArrayList is not needed to be known anywhere else.
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
List list = new ArrayList();

Here you are creating new object of ArrayList ok, after your development is done in testing phase if you found that ArraList is performing badly you want to have LinkedList. since List interface is implemeted by both you can easily replace the above code with
List list=new LinkedList();
without breadking any functionality
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic