Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason for - List list = new ArrayList(); [duplicate]

I have seen code like this many times:

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

Why do people take the parent of ArrayList (and other classes) instead of the type of the generated object?

Does that take less performance? Or why should someone do this?

like image 736
codepleb Avatar asked Aug 20 '13 07:08

codepleb


People also ask

Why do we do list new ArrayList?

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.

Why does my ArrayList contain N copies of the first item added to the list?

This problem has two typical causes: Static fields used by the objects you stored in the list. Accidentally adding the same object to the list.

When an ArrayList is filled the old array contents will be copied to the new array is it a shallow copy or deep copy?

ArrayList class returns a shallow copy of this ArrayList instance (i.e the elements themselves are not copied). Using this method, you can copy the contents of one array list to other.


2 Answers

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.

like image 177
MD Sayem Ahmed Avatar answered Sep 29 '22 13:09

MD Sayem Ahmed


When you write:

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

Then you are sure you'll use only the functionality of the interface List.
(ArrayList implements List, so List is more flexibl). Using this, allows you to change the ArrayList to other types in the future (like LinkedList..).

like image 26
Maroun Avatar answered Sep 29 '22 13:09

Maroun