Иван Бишевац
Иван Бишевац

Reputation: 14631

Confusion about declaring object

I am learning Collections in CoreJava book and I found this code:

      List<String> a = new LinkedList<String>();

Now I wonder why this code isn't like this:

LinkedList<String> a = new LinkedList<String>();

Why we declare a as List?

Upvotes: 0

Views: 141

Answers (5)

Nyal Fernandes
Nyal Fernandes

Reputation: 46

In Java, like all other OOP languages, one of the major strengths in designing is Polymorphism. Polymorphism just does not include methods but extends to Classes as well and takes on names like subtyping, polytypism, etc.

In your first code example:

List<String> a = new LinkedList<String>();

Makes the type of reference variable 'a' take any concrete extensions of type 'List'.

Where as in the next code sample:

LinkedList<String> a = new LinkedList<String>();

Reference variable 'a' can take any extensions of type 'LinkedList'.

To understand the power of this we should check the type hierarchy of LinkedList: http://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

So in your first example you increase your scope of referring 'a' to any concrete type of 'List', which also includes LinkedList, where as in the second example you decrease your scope to only type 'LikedList' and its subtypes.

One is not better than the other but a lot is depedant on your code design and application as to which approach fits your use case best.

Some good reads:

Subtyping Polymorphism: http://en.wikipedia.org/wiki/Polymorphism_(computer_science)#Subtyping

Upvotes: 0

Muhammad Saifuddin
Muhammad Saifuddin

Reputation: 1294

another good example of.

Why we declare a as List?

Consider a method:

public void doStuff(LinkedList llObj){} 

This shows restriction to the method that users only allow to pass a LinkedList.

public void doStuff(List listObj) {} 

while this method allows any List to be passed.

hope this helps.

Upvotes: 1

wannik
wannik

Reputation: 12696

The code:

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

is better than:

LinkedList<String> list = new LinkedList<String>();

This is because if you declared list as List and you found that the performance of the program is not good, you could change the code to:

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

and see if the program runs faster. The beauty of this is that you can make that change without having to change the rest of the code.

As a conclusion, if possible, declare a reference as an interface (List is an interface):

List<String> list;

and you can easily switch the implementation (class ArrayList and class LinkedList are implementations of the interface List):

list = new LinkedList<String>();

or

list = new ArrayList<String>();

Upvotes: 3

Ted Hopp
Ted Hopp

Reputation: 234795

Your second would work and might be preferable if you needed to use operations specific to LinkedList. But this need is usually quite rare. The first is more general -- it allows you to use a different List implementation by changing just one line. Other code (like method parameters) wouldn't need to be changed.

P.S. Just to be clear - a LinkedList is a concrete implementation of List.

Upvotes: 4

Dexygen
Dexygen

Reputation: 12561

List is the interface, LinkedList is a specific implementation of the interface. "Program to an 'interface', not an 'implementation': http://en.wikipedia.org/wiki/Design_Patterns#Introduction.2C_Chapter_1

Upvotes: 4

Related Questions