2

Possible Duplicate:
ArrayList declaration Java

In Java, why is an ArrayList often declared using a List?

List<String> strList1 = new ArrayList<String>();
ArrayList<String> strList2 = new ArrayList<String>();

I am not able to understand the difference between these two. Is one better than the other? or have a specific use?

Community
  • 1
  • 1
rgamber
  • 5,749
  • 10
  • 55
  • 99

7 Answers7

5

The ArrayList class implements the List interface.

It is usually a good practice to declare an object of the type of the interface and of course instantiate it using an implementation of that interface.

This way, you can change the implementation when needed, but the declared type stays unchanged. This way, you could avoid modifying too much code especially in the methods getting the variable as a parameter. The method keeps getting a List as a parameter, not a certain implementation.

Dan D.
  • 32,246
  • 5
  • 63
  • 79
4

In Java, a List in an interface, while ArrayList is one of several classes that implement that interface. The advantage of the first notation is that you can easily change which implementation you use without having to change the rest of your code. For example, you might later decide to use a Vector instead of an ArrayList, so you could just change the code to look like:

List<String> strList1 = new Vector<String>();
Darius Makaitis
  • 880
  • 1
  • 5
  • 5
1

The idea here is that you're designing to the interface instead of the concrete object itself. Interfaces are more flexible in the sense that you can later swap out strList1 for a LinkedList if you so desired or needed to.

Makoto
  • 104,088
  • 27
  • 192
  • 230
1

Liscov Substitution Principle

Substitutability is a principle in object-oriented programming. It states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may be substituted for objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.).

ArrayList is a subtype of List.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
Nishant
  • 54,584
  • 13
  • 112
  • 127
1

Its not Particulary about this question, but it is always recommended to use interface rather than Class for holding refrence, this allows you to change implementation any time you need. For eg you can any time change it to Linked list

ManMohan Vyas
  • 4,004
  • 4
  • 27
  • 40
1

When you keep your code bind to the interface you can very easily change the implementation class when required.

If you code by the Concrete class you may use features specific to the class there by making your code bind to that class. And future replacement becomes difficult.

To understand the feature better think of a class coming from third parties. You don't need to depend your code on any particular vendor. You can change the class to a different vendor when you please. Both are bound by the interface so change is easy.

Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47
1

I think that basically, the previous replies are saying that you can upcast a List to an ArrayList or another object that also implements the List interface.

Glade Mellor
  • 1,326
  • 17
  • 10