13

I keep coming across code where people instantiate a new ArrayList and assign it to the List interface, like this:

List<String> names = new ArrayList<String>();

What is the reasoning behind this approach?

S-K'
  • 3,188
  • 8
  • 37
  • 50

4 Answers4

19

To decouple your code from a specific implementation of the interface.

This also helps you to move to another implementation of the List interface in the future.

For example -

You have List<String> names = new ArrayList<String>(); later on you decide that you should have used some other implementation of the List interface, say LinkedList so you would just change it to List<String> names = new LinkedList<String>(); and nothing breaks.

JHS
  • 7,761
  • 2
  • 29
  • 53
  • So ArrayList could be casted into another implementation of List if required later? – S-K' Jul 03 '13 at 23:13
  • 1
    It could be *replaced* with another implementation of List. – user2357112 Jul 03 '13 at 23:17
  • 6
    `Vector` is a legacy, obsolete, ugly, hateful class that one shouldn't use anymore. – Lion Jul 03 '13 at 23:21
  • :) Even I do not like it. But thats the closest to ArrayList in terms of its properties. – JHS Jul 03 '13 at 23:22
  • It's not only about being able to switch implementations. It makes your code more generic, flexible, and reusable. For example, you can write a utility method which operates on an object of type List, and this method will work for *all* valid list implementations. This is extremely important in library code, but the same principle applies to application code. – JimN Jul 03 '13 at 23:45
  • @JimN - You are right. It allows encapsulation too. One can keep adding the benefits of using the interface instead of the real implementation. – JHS Jul 03 '13 at 23:47
4

Programming to an interface, not an implementation

It's a design pattern from Gang of Four (GoF). Why is that?

You guide by abstract contract instead of concrete implementation.

public class MyClass {

    private List myList ;

    public setMyList(List list){
        myList=list;
    }

}

what about instead of ArrayList implementation you want LinkedList implementation? in that way you only has to inject that property with the setter.

Abstraction is the key, you don't know nothing about implementation only guide by specification.

Read this What does it mean programming to an interface?

OLIVER.KOO
  • 5,654
  • 3
  • 30
  • 62
nachokk
  • 14,363
  • 4
  • 24
  • 53
4
List<String> names = new ArrayList<String>();

With that you write code against the interface List, which makes it easy to switch the implementation in the future if you have to.

In that case the following will suffice -

List<String> names = new //some other implementation of List

Now if you do something as follows -

ArrayList<String> names = new ArrayList<String>();

You will code against the implementation ArrayList itself. And your code is tied to that specific implementation. In case if you have to switch the implementation then it will require a lot of code changes.

Check the docs to discover some of the standard implemenation provided by Java 6.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
4

Such code doesn't use the List class, it declares the variable as being of type List. This is called abstraction

List is an interface, which defines what methods the actual class has, without specifying which actual class is used.

Although not a big deal here, this practice can be very important, even essential in more complexes situations. It's good practice to always follow this pattern.

Bohemian
  • 412,405
  • 93
  • 575
  • 722