Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple implements same interface

 
Nanchun Lin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everybody,
I find that several classes in the java.util pacakage implements same interface several times. For example,
LinkedList extends AbstractSequentialList implements List, Cloneable, Serializable
AbstractSequentialList extends AbstractList
AbstractList extends AbstractCollection implements List
So, LinkedList implements List twice, one directly, one indirectly.
Can anybody give me an explanation?
Thanks,
Nanchun
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AbstractList most likely implements a default behavior for methods defined in the List interface.
Your classes like ArrayList, LinkedList, etc. that implement List also probably define a more specific implementation of the methods defined in List, so override the default provided by AbstractList.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nanchun Lin:
Can anybody give me an explanation?
What kind of explanation do you want? How about it makes it a lot easier to see that LinkedList implements List by putting it directly in the class defintion.
 
Nanchun Lin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Jason and Thomas,
Thank you very much for giving me a reply.
What I want to know is why Sun writes "implements List" again in LinkedList class? Is there any other reason than for convenience? One of my friends told me that it may improve the performance. If so, why LinkedList does not implement Collection interface again?
If LinkedList wants to provide some specific implementation of List interface, it can override the functions directly and it need not write the "implements List" again.
Nanchun
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general idea is that if a class extends a class that implements an interface, the the hierarchy of interfaces is not clearly evident. It makes it much easier to see if the interface is directly listed. It has nothing to do with performance.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nanchun- true, since the method is already there you can just override.
I guess having the implements List there, even though it already implements it, is an easier way for someone looking through the API to go directly to the interface involved, like Thomas is saying, versus having to traverse through the extended class and its interfaces. A little uncomfortable with that, but it does make some sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic