3

So many time I have seen that instantiation of Arraylist is done in the manner "

List<Object> listObject = new ArrayList<Object>();

So I am wondered that what is the significance of instantiation of Arraylist in this way? What happens if we instantiate ArrayList() like

ArrayList<Object> listObject = new ArrayList<Object>();
Santosh
  • 17,667
  • 4
  • 54
  • 79
Pankaj Deshpande
  • 502
  • 2
  • 5
  • 15
  • You can look this topic. http://stackoverflow.com/questions/2279030/type-list-vs-type-arraylist-in-java – dijkstra Jun 12 '13 at 06:50
  • possible duplicate of [What does it mean to "program to an interface"?](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – Eng.Fouad Jun 12 '13 at 06:50

10 Answers10

9

List is an interface , ArrayList class is a specific implementation of that interface.

List<Object> listObject = new ArrayList<Object>();

With this you can change the List implementation in future. List listObject can invoke all the methods declared in the List interface. In future , if you don't want the ArrayList implementation of the List, and change it with say a LinkedList , you can do that :

List<Object> listObject = new LinkedList<Object>();

You will not have to alter the code which uses listObject , if you had declared the listObject as List interface type, and not worry about it breaking the rest of the code because you might have used something specific to ArrayListwith this declaration:

ArrayList<Object> listObject = new ArrayList<Object>();

This is called Programming to an interface, not to an implementation.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
3

This is because of the fact that its always a good practice to write code to interface and not implementation. So when you do List<Object> listObject = new ArrayList<Object>(); you always have the liberty to change ArrayList to LinkedList and elsewhere in the code will need no change. So programming to interface (List) here gives you the liberty/power to change the underlying implementation without affecting other places in code. I will suggest you to read this small note to have more clarity.

Saurabh
  • 7,894
  • 2
  • 23
  • 31
2

Most of answers refer to the mantra of "coding to the interface". Although, it is a sound advice, it might lead to some problems. Specifically, in case of List the user of object listObject would not know anything about the efficiency of the underlying data structure. For example, if this is an instance of ArrayList then getting the last element is only O(1), while if it is a LinkedList then it is O(listObject.size).

Therefore, often it is very important to know the exact underlying data structure, which can be achieved by using the implementation type for variable declaration. For instance, Scala's List is a class that make its data structure explicit.

Thus, I would say that as always, the decision should be an informed one and cater toward the design of your application. If the data structure should be "a list" and no requirements for specific implementation is known then use List for declaration. However, if a set of algorithms that should work on that data structure is well known then use a concrete implementation class (i.e. ArraysList, LinkedList) that would ensure optimal performance of those algorithms.

01es
  • 5,362
  • 1
  • 31
  • 40
1

There's no difference between list implementations in both of your examples. There's however a difference in a way you can further use variable myList in your code.

When you define your list as:

 List myList = new ArrayList();

you can only call methods and reference members that belong to List class. If you define it as:

ArrayList myList = new ArrayList();

you'll be able to invoke ArrayList specific methods and use ArrayList specific members in addition to those inherited from List.

Nevertheless, when you call a method of a List class in the first example, which was overridden in ArrayList, then method from ArrayList will be called not the one in the List.

That's called polymorphism.

shreyansh jogi
  • 2,082
  • 12
  • 20
1

This will ,"Program to an interface and not to an Implementation"

Please have a look at this and this

Community
  • 1
  • 1
Vikas V
  • 3,176
  • 2
  • 37
  • 60
1

List is an interface, and ArrayList is a class. Best practices say: Program against interfaces, not concrete classes.

Actually, there may be no difference between those two definitions (defining the variable type as interface vs class), but using List makes your code dependent on the interface, not the class. Then you can later change the actual type of the variable, without affecting the code that is using the variable.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
1

Because you can use different implementations of the same interface(in this case you may want to change your List implementation to a linkedList instead of an ArrayList ) without changing code.

Why should the interface for a Java class be preferred?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

As of Java 7 these are the known implementations of List

All Known Implementing Classes:
AbstractList, AbstractSequentialList, ArrayList, AttributeList, CopyOnWriteArrayList, LinkedList, RoleList, RoleUnresolvedList, Stack, Vector

Leaving aside the Abstract classes what this means is that in the future if you want to plug in a different List implementation other than ArrayList you have a variety of options.

Also called Coding to Interfaces this is a widely used programming paradigm.

Ajay George
  • 11,759
  • 1
  • 40
  • 48
  • From all above discussion I understood that "Coding to Interface" gives us liberty to use various implementations of classes which implement that interface. So it reduces the overhead of taking care of implementing class and its methods. We can interchange betwwen them runtime. Like here, we can use LinkedList also wherever required than Arraylist. – Pankaj Deshpande Jun 12 '13 at 07:16
  • @PankajDeshpande Spot on ! – Ajay George Jun 12 '13 at 07:40
1

When you use List you use Abstraction. Imaging a case wherein there is function call expects List<Object>

List<Object> myList = myFunction();

now in function myFunction(), its better to use

List<Object> myList = new ArrayList<Object>();
return myList;

than

ArrayList<Object> myList = new ArrayList<Object>();

because you will avoid unnecessary casting. The calling function only expects a List irrespective of its implementation.

Moreover, List interface offers a lot of generic functions which are applicable for all the implementations. So you can write some generic code manipulating a list irrespective of whether its LinkedList or ArrayList. This is the advantage of using Abstraction.

Santosh
  • 17,667
  • 4
  • 54
  • 79
1

List is an Interface. when ever you define interface as reference we have to instantiate using it's implementation.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115