Pankaj Deshpande
Pankaj Deshpande

Reputation: 502

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

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>();

Upvotes: 3

Views: 12171

Answers (10)

01es
01es

Reputation: 5410

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.

Upvotes: 2

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35587

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

Upvotes: 1

Santosh
Santosh

Reputation: 17923

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.

Upvotes: 1

Ajay George
Ajay George

Reputation: 11875

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.

Upvotes: 1

AllTooSir
AllTooSir

Reputation: 49432

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.

Upvotes: 9

Suresh Atta
Suresh Atta

Reputation: 122026

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?

Upvotes: 1

Moha Dehghan
Moha Dehghan

Reputation: 18472

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.

Upvotes: 1

Saurabh
Saurabh

Reputation: 7964

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.

Upvotes: 3

Vikas V
Vikas V

Reputation: 3196

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

Please have a look at this and this

Upvotes: 1

shreyansh jogi
shreyansh jogi

Reputation: 2102

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.

Upvotes: 1

Related Questions