aspose file tools
The moose likes Java in General and the fly likes Inheritance in List Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Inheritance in List Interface" Watch "Inheritance in List Interface" New topic
Author

Inheritance in List Interface

Aby Krishna
Greenhorn

Joined: Jul 10, 2010
Posts: 7
Hi,

I am looking in to the source code for JDK (6.0), and have the following question over the List interface. Please clarify on what is wrong in my understanding.

List interface inherits Collection interface. Hence all the public/package-private methods declared in Collection interface would be inherited in the List interface and would be required to be implemented in the concrete implementation for List.

Then why is it required to declare the same methods in List interface which are already declared in Collection interface and inherited? (for eg: int size() and boolean isEmpty() are declared in the Collection interface and List interface both).

Your help is appreciated.

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Check this thread.

In short: syntactically these re-declarations add nothing at all, but semantically / contractually they do.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Aby Krishna
Greenhorn

Joined: Jul 10, 2010
Posts: 7
Thanks, that was helpful.

Okay another question on the same topic:

Why does ArrayList implment List interface when AbstractList already implements List interface and is inherited by ArrayList?

I understand that not all methods from List are implemented in AbstractList, but since ArrayList extends AbstractList, all the method implementations and abstract declarations would be automatically inherited, in addition the abstract declarations would be mandatory to be implemented in ArrayList(unless ArrayList is declared abstract too).

Am i missing something? or is it again "for contract and convenience" purpose that List interface is implemented again in ArrayList?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You're right that it isn't necessary. It's probably done for clearity -- you can see ArrayList implements List without having to check AbstractList.

Although even with this reasoning it isn't necessary. The Javadoc already shows all implemented interfaces, also Iterable and Collection, so specifying it again really serves no purpose.
Aby Krishna
Greenhorn

Joined: Jul 10, 2010
Posts: 7
Cool, thanks.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Inheritance in List Interface
 
Similar Threads
Collection interface methods
Method Overriding Interface vs. Class
List interface and toString( ) method
Abstract cannot be protected
Does static methods and variables get inherited ?