10

I am new to java and when I go through the code of many examples on net I see people declaring the variable for an ArrayList as simply List in almost all the cases.

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

I don't understand if there is some specific advantage of doing this. Why can't it be an ArrayList itself, like so:

ArrayList<String> myList = new ArrayList<String>();
Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
Rasmus
  • 8,248
  • 12
  • 48
  • 72
  • 2
    You will need to change your language: This does not involve casting in the least. Nowhere do you see a cast, `(List)` being used. – Hovercraft Full Of Eels Jun 13 '12 at 02:29
  • 1
    I think it's covered: http://stackoverflow.com/questions/4062982/list-versus-arraylist , http://stackoverflow.com/questions/9852831/why-use-list-list-new-arraylist-instead-of-arraylist-list-new-arraylist , http://stackoverflow.com/questions/147468/why-should-the-interface-for-a-java-class-be-prefered :-) –  Jun 13 '12 at 02:31

6 Answers6

14

It's called programming to an interface. It allows you to substitute that ArrayList for a LinkedList if somewhere down the line you decide that a LinkedList would be more appropriate.

Community
  • 1
  • 1
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • 3
    Though, the first sentence is OK, ... _It allows you to substitute that ArrayList for a LinkedList if somewhere down the line you decide that a LinkedList would be more appropriate._ does not sound right – Op De Cirkel Jun 13 '12 at 02:34
  • 1
    @OpDeCirkel What sounds wrong with it? That is one of the advantages of programming to an interface. – Jeffrey Jun 13 '12 at 02:36
4

Because if you decide to change it later it's easier:

List<String> myList = new ArrayList<String>();
List<String> myList = new LinkedList<String>();

It's more important in method signatures, when changing what you return can force everyone using your function to change their code.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
2

This isn't a cast but rather you are using the interface as the declared type of the variable. The reason for doing this is that you can switch which implementation of List you are using. If users of the list only depend on the List interface, they won't be affected if you using another list type, such as LinkedList.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • I guess, instead of saying _interface as the declared type_, you want to say _interface as the declared type of a variable_. – Op De Cirkel Jun 13 '12 at 02:31
2

List is an interface so it can store referance to all classes extending from List Interface.If you used following statment then after some programing steps you can also store other Objects(extending from List) referance.

 List<String> myList = new ArrayList<String>(); 
 ...
 myList =new LinkedList<String>();

The main advantage comes with this is code maintenance. For instance, if someone comes and wants to use a LinkedList instead of the ArrayList, you only need to change one line rather than zillions throughout the code. It makes your code a little more flexible.

The whole concept of polymorphism in java relies on java's ability to use interface/base class reference to reference to sub class objects.

Also, programming to an interface rather than to the actual implementation is always seen as a best practice. This would allow the developer to replace one implementation with other/better implementation easily when required.

swapy
  • 1,616
  • 1
  • 15
  • 31
1

Java is strongly typed object-oriented language. .... Let's go through an example: Suppose that you are writing a sort function sort(ArrayList<> list). If the function declared in that way, java wont allow you to sort anything else with that function than ArrayList. Better approach would be to define an abstraction (as in your question is) List. In that abstraction you declare methods. that all implementations must implement-define (ie ArrayList). Now your sort function can be declared as sort(List<> list) and you can sort with it anything that implements List (and follows the contract of the List methods).
In this way you can reuse code that you wrote for all your implementations as long as they obey the contract of the abstraction. In Java you achieve this using interfaces as abstractions and classes as implementations. There are other advantages too, but you have read some book about Object-oriented programming if you want to understand them better.

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
0

1) List is an interface, the implementation class is ArrayList
2) List interface extends Collection.

Cassian
  • 3,648
  • 1
  • 29
  • 40