This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Multiple implements same interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple implements same interface" Watch "Multiple implements same interface" New topic
Author

Multiple implements same interface

Nanchun Lin
Greenhorn

Joined: Nov 13, 2001
Posts: 3
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

Joined: May 08, 2001
Posts: 1932
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

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Nanchun Lin
Greenhorn

Joined: Nov 13, 2001
Posts: 3
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

Joined: May 05, 2000
Posts: 13974
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

Joined: May 08, 2001
Posts: 1932
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Multiple implements same interface
 
Similar Threads
LinkedHashMap extends HashMap implements Map
Interfaces
Cast LinkedList to ArrayList, enjoy the class cast exception
Collection
Assigning arraylist variable to a method which returns of type AL