Polymorphism: Why Use "List List = New Arraylist" Instead of "Arraylist List = New Arraylist"

Polymorphism: Why use List list = new ArrayList instead of ArrayList list = new ArrayList ?

The main reason you'd do this is to decouple your code from a specific implementation of the interface. When you write your code like this:

List list = new ArrayList();  

the rest of your code only knows that data is of type List, which is preferable because it allows you to switch between different implementations of the List interface with ease.

For instance, say you were writing a fairly large 3rd party library, and say that you decided to implement the core of your library with a LinkedList. If your library relies heavily on accessing elements in these lists, then eventually you'll find that you've made a poor design decision; you'll realize that you should have used an ArrayList (which gives O(1) access time) instead of a LinkedList (which gives O(n) access time). Assuming you have been programming to an interface, making such a change is easy. You would simply change the instance of List from,

List list = new LinkedList();

to

List list = new ArrayList();  

and you know that this will work because you have written your code to follow the contract provided by the List interface.

On the other hand, if you had implemented the core of your library using LinkedList list = new LinkedList(), making such a change wouldn't be as easy, as there is no guarantee that the rest of your code doesn't make use of methods specific to the LinkedList class.

All in all, the choice is simply a matter of design... but this kind of design is very important (especially when working on large projects), as it will allow you to make implementation-specific changes later without breaking existing code.

Reason for - List list = new ArrayList();

When someone writes code like this, he/she is trying to follow a basic OO design principle which says -

Program to an interface, not to a concrete implementation

I have explained this principle in one of my blog posts. Look in the Class Inheritance VS Interface Inheritance section.

To summarize the post, when you use a reference of a parent type to refer to an instance of a sub-type, you get a lot of flexibility. For example, if you ever need to change your sub-type implementation in the future, you will be able to do that easily, without changing much of your code.

Consider the following method -

public void DoSomeStuff(Super s) {
s.someMethod();
}

and a call to this method -

DoSomeStuff(new Sub());

now, if you ever need to change the logic inside someMethod, you can easily do it by declaring a new subtype of Super, say NewSubType, and changing the logic inside that implementation. In this way, you will never have to touch other existing code which utilizes that method. You will still be able to use your DoSomeStuff method in the following way -

DoSomeStuff(new NewSubType());

Had you declared the parameter of DoSomeStuff to be of Sub, you would then have to change its implementation too -

DoSomeStuff(NewSubType s) {
s.someMethod();
}

and it may also chain/bubble to several other places.

In terms of your collection example, this lets you change the list implementation that a variable is pointing to without much hassle. You can easily use a LinkedList in place of an ArrayList.

Java polymorphism (ArrayList and List)

This is code to interface. Here you can see the assignment is done to a List interface not the ArrayList class which is implementing the List. ArrayList, LinkedList implements List interface, the same way, you can have your own List implementing Class as well. So, in future if you want to change the implementation in such a way that instead of ArrayList object you want some other List implementation like LinkedList then you can easily modify the code like this -

 List<List<Individual>> group = new ArrayList<List<Individual>>(4); 
to
List<List<Individual>> group = new LinkedList<List<Individual>>(4)

This change will have no impact on the other part of your code which uses group variable as for other this is a List object not an Arraylist or LinkedList object. It is not going to break your code and you don't have to waste your time to modify your code to accomodate this change.

Why assign a new ArrayList to a List variable?

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.

ArrayList and List Interface Polymorphism?

Polymorphism work in the reverse order of what you understood, means that any object of a parent class/interface (in this case a List) can hold value of a child class (or a class that implements the interface):

List a = new ArrayList(); //correct
ArrayList b = new List(); //incorrect

so how can it return a List<> object?

You can use interface as a return type. Read this question for clarification.



Related Topics



Leave a reply



Submit