1

Possible Duplicate:
Why should the interface for a Java class be prefered?

In Java, is there a difference between these two lines? If so, what is it, and when should I use either one? Is there a preferred convention?

Note that ArrayList implements List.

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

ArrayList<String> bunchOfStrings = new ArrayList<String>();
Community
  • 1
  • 1
smcg
  • 3,195
  • 2
  • 30
  • 40

5 Answers5

2

In Java, is there a difference between these two lines?

Yes, there is a slight difference, and the List<String> variant is preferred.

Is there a preferred convention?

The convention says that you should "program against interfaces". The reason is that it becomes much easier to swap the actual implementation (in this case from ArrayList to say LinkedList).

aioobe
  • 413,195
  • 112
  • 811
  • 826
1

You should use the most generic type possible. So in this case that's List. The only time you would use a more specific type is if that type had methods defined on it that the more generic type does not.

The reason is that if a method accepts a type, like List, the user of the method can provide any type of List they want. If a method only accepts an ArrayList, the user can't use any other type of List.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
1

In both case you are instantiating an ArrayList. The difference is that in the first case you are referring it as a List (the interface that ArrayList implements ), so you don't have access to specific method of ArrayList class, but only interfaces method.

use thi first:

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

This way you can switch different List implementation (ArrayList, Vector):

public class AClass(){
   List<String> bunchOfStrings;

   public List<String> getList(){
        bunchOfStrings = new ArrayList<String>();
        //or you can switch to : bunchOfStrings = new Vector<String>();
        return bunchOfString;
   }
}
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
0

If you declare bunchOfStrings as a List, you can't call any of the ArrayList methods (without casting), even though it is pointing to an ArrayList object -- you can only call methods that are declared in the List class.

For this reason, you might think that it's better to always declare your variable type to be as specific as possible, but declaring it as a List is generally preferable because it allows you to change the underlying implementation of your List without having to change anything else in your code. You can easily modify your code as follows without breaking anything else in your code:

List<String> bunchOfStrings = new LinkedList<String>();

As a rule of thumb, only ever declare the type of your variable to be a specific subclass if you...

  1. ...need access to methods that are only available in the subclass.
  2. ...need to pass the object instance to a method that only accepts objects of that subclass.
Mansoor Siddiqui
  • 20,853
  • 10
  • 48
  • 67
0

This is called Polymorphism. It is very useful and has no real drawbacks.

Pros:

You can send this instance variable to any method that wants a "List" object.

Using List in a method declaration, you are opening up the method to anything that is a sub-type of List, instead of just of type ArrayList.

Cons:

You would have to cast your variable to an ArrayList to access or set any features/methods/fields that are in ArrayList (but not in List). But those things are still available.

PaulG
  • 466
  • 1
  • 6
  • 19