• Post Reply Bookmark Topic Watch Topic
  • New Topic

Linked List implementing two Interfaces. Why ??  RSS feed

 
Ranch Hand
Posts: 105
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

As you all can see in the image , Linked list is the only class which is implementing two interface , List as well as Queue.

Why so ??

i am not able to get this point ..

Need explanation on that...

Any Explanation will be Highly Appreciated.

Thanking You in Advance.
Collection.jpg
[Thumbnail for Collection.jpg]
 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I always say that it is best to start learning Java with Java core!

So, learn at first why java class implements interface, then why java class implements two interfaces and why java class extends anothe java class.
I learned these things fom Bruce Eckel 'Thinking in Java'.

LinkedList is another java class that implements two interfaces...
 
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the tutorial here on interfaces,

Your class can implement more than one interface, so the implements keyword is followed by a comma-separated list of the interfaces implemented by the class


So yes a class can implement multiple interfaces. It is an allowed behavior. Though you will be hard pressed to find more than 4 interfaces declared in the `implements` clause in the most of the Java API classes (Correct me if I am wrong).
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface just defines some functionality that a class that implements it must have. If you have a class that provides functionality in specified by multiple interfaces, why not declare all of them? Why should it be a problem? After all, interfaces are Java's approach to allowing some form of multiple-inheritance - part of the whole point of them is that you can do this sort of thing.

In that particular case, LinkedList can act as a List or as a Queue. In fact, it also implements Deque as well - and lots of people thing that the Deque interface really ought to be broken up even further.

As for being the the only class: check out the documentation for ArrayList:

All Implemented Interfaces:

Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess
 
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope it is obvious why a linked list is a List. As for a Queue, that is intended to work on data structures organised like this
sdfks→jsdfj→uygwfh→hbdjd→vy7
Those are bits of date (created by hitting the keyboard haphazardly ‍) joined by →arrows. You can add something on the left end, and take something off the right end. That is how a Queue works. Add something at one end and take things off the other end.
If you look up diagrams of linked lists, you will find they look like my structure above, and will probably have arrows like this←, too. So a linked list is a good structure for a Queue. So the java.util.LinkedList class has methods from the List interface and methods from the Queue interface. So a linked list can implement a List interface and a Queue interface.
 
Rohit Kumar Singh
Ranch Hand
Posts: 105
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:Hello!

I always say that it is best to start learning Java with Java core!

So, learn at first why java class implements interface, then why java class implements two interfaces and why java class extends another java class.
I learned these things from Bruce Eckel 'Thinking in Java'.

Linked List is another java class that implements two interfaces...


@all,

I am under no illusion how an interface work's and how it is implemented with the classes and how we achieve Multiple Inheritance in java using Interfaces..

My First question is why only Linked List , why not Arraylist ???

What made the API Developer's (Joshua Bloch), think that only Linked List should implement more than one interface...

As we know from Data Structure that a Linked List can be used as a Stack as well as a Queue..

Here goes my Second question, "Why there is no Stack Interface or Class in Collection API"

I think you all got my Point.
Thanks in Advance.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rana Rananjay Singh wrote:My First question is why only Linked List , why not Arraylist ???

Errm. See Matthew Brown's answer.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use interface in order to make different classes provide the same method or methods.
Then I write a method that accepts my interface and calls its method. Therefore my method can accept many different classes which implement that interface.
With interfaces I make one class have many types.
For instance I have interface DBManager with one method readData(). Then I can create class MySQLManager and class OracleManager, they both implement DBManager. And they both provide method readData() but first class performs readData() with mysql and second with Oracle database.

Now third class DBReader can use object of type DBManager to call readData() method. Type DBManager can refer to both MySQLManager or OracleManager class. So I do readData() with one type DBManager but not two types MySQLManager and OracleManager.
 
Rohit Kumar Singh
Ranch Hand
Posts: 105
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:An interface just defines some functionality that a class that implements it must have. If you have a class that provides functionality in specified by multiple interfaces, why not declare all of them? Why should it be a problem? After all, interfaces are Java's approach to allowing some form of multiple-inheritance - part of the whole point of them is that you can do this sort of thing.


Sorry haven't seen this one.

Thumb's Up For this Answer.
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rana I completely forgot that you learn java collections.

For those who want to learn Java Collections in depth I prepared Internal life of Java Collections series.
 
Rohit Kumar Singh
Ranch Hand
Posts: 105
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:@Rana I completely forgot that you learn java collections.

For those who want to learn Java Collections in depth I prepared Internal life of Java Collections series.


I checked that Blog Buddy..


Well Written ..

All the Best with your Career...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!