23

Ok so I know that Set, List and Map are interfaces but what makes the first line of code any better than the second line?

List myArr = new ArrayList();
ArrayList myArr = new ArrayList();
2
  • 1
    Second line is better because ArrayList type provides more functionality in addition to List. However the List type is better when you do code that does not depend on List implementation. Please cover my Internal life of ArrayList to know it deeper. Commented Aug 5, 2013 at 14:13
  • 1
    @VolodymyrLevytskyi page does not exist, first is good too Commented Sep 24, 2019 at 7:15

3 Answers 3

38

If you use the first form, you are saying all you are ever going to use is the functionality of the List interface - nothing else, especially nothing extra added by any implementation of it. This means you can easily change the implementation used (e.g. just substitute LinkedList for ArrayList in the instantiation), and not worry about it breaking the rest of the code because you might have used something specific to ArrayList.

Sign up to request clarification or add additional context in comments.

2 Comments

I think the case can be made more compelling when you mention wrapping your ArrayList in a Collections.checkedList() for debugging. When you mention LinkedList, people say "why would I ever use LinkedList?".
@ILMTitan: Agreed. That is a far more common scenario than having to switch to LinkedList. But LinkedList is useful too, I have had to switch to using them whenever I needed to make lots of adds/removes to the beginning. The main point here is to separate the implementation and the interface so that the implementation can change without having to make lots of changes to the rest of the code.
16

A useful general principle about types in programming (sometime referred to as the robustness principle) is as follows:

  • Be liberal about what you accept
  • Be conservative about what you emit

List is more liberal than ArrayList, since List can be any kind of List implementation e.g. an ArrayList, a LinkedList or FrancosSpecialList. Hence it is a good idea to be liberal and accept any kind of list since you may want to change the implementation later.

The main reason to use ArrayList explicitly as a type (your second case) is if you need to use methods that are specific to ArrayList that are not available through the List interface. In this case a generic List won't work (unless you want to do lots of ugly and confusing casting), so you might as well be explicit and use an ArrayList directly. This has the added bonus of hinting to a reader that specific features of ArrayList are needed.

Comments

7

As you can see from the source of ArrayList here, most of the methods implemented are annotated as @override because all of them that are defined through List interface so, if you are gonna use just basic functionalities (that is what you are gonna do most of the time) the difference won't be any practical one.

The difference will come if someday you will think that the features of the ArrayList are not suitable anymore for your kind of problem and you will need something different (a LinkedList for example). If you declared everything as List but instantiated as ArrayList you will easily switch to new implementation by changing the instantiations to new LinkedList() while in other case you will have to change also all variable declarations.

Using List list = new ArrayList() is more OOP style since you declare that you don't care about the specific implementation of the list, and that you want to discard the static information about the type since you will rely on the interface provided by this kind of collection abstracting from its implementation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.