parth
parth

Reputation: 272

LinkedList different implementations

I have seen 2 valid declarations of the LinkedList class in Collections Framework in Java.

  1. LinkedList a = new LinkedList();
  2. Queue a = new LinkedList();

What exactly is the difference between these 2 implementations?

Upvotes: 3

Views: 132

Answers (3)

Kick
Kick

Reputation: 4923

Below is the code of LinkedList

public class LinkedList<E> extends AbstractSequentialList<E>
  implements List<E>, Deque<E>, Cloneable, Serializable

So as you can see this class implements Deque<E> and so on this interface extends Queue<E> interface so you can create the object as below :

Queue B = new LinkedList(); 
Deque d = new LinkedList(); 

In above declaration LinkedList object is refer by the Queue interface, it mean you can call all the methods declare in Queue.

Upvotes: 0

Ivan
Ivan

Reputation: 1517

There is just one implementation in your code: LinkedList.

Queue is one of the multiple interfaces implemented by LinkedList class.

Upvotes: 1

Queue is an interface which LinkedList class implements.

In Your first case you a is a LinkedList reference to a LinkedList object. In the latter, a is a Queue interface reference pointing to a LinkedList object so, in this last case you will be only able to execute those service of LinkedList which are included in Queue interface.

Upvotes: 3

Related Questions