• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple implements same interface

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic