I am curious why some collection classes has extended abstract class and at the same time implementing interfaces there corresponding collection interface like List,Set etc.?. Why not only extending abstract class. I can understand implementing other interface for adding functionality like Cloneble, Serializable, etc. But extending abstract class and also implementing interface which already abstract class has implemented . I think there is some design principle which I am missing. Please someone can elaborate this design reason?
Asked
Active
Viewed 204 times
2 Answers
3
The collection interfaces exist so that people can consume collections without knowing anything about what type of collection it actually is – even if it doesn't extend the any of abstract classes.
For example, most of the concurrent collections do not extend the abstract base classes.
The abstract classes exist to provide common base functionality for most implementations.

SLaks
- 868,454
- 176
- 1,908
- 1,964
-
1To expand a little on this, the collection interfaces typically contain methods that can be easily represented in terms of a few base methods. The abstract class provides those implementations, allowing a collection to be implemented by writing far fewer methods than the interface contains. – Patricia Shanahan May 19 '13 at 13:28
-
@SLaks _**"The collection interfaces exist so that people can consume collections without knowing anything about what type of collection it actually is"**_ I agreed on this but doesn't mean that concrete implementation has to be a child of both Abstract and Interface. Being just child of abstract classes seems enough. You can send concrete classes wherever there is reference of Interface since abstract has already implementing it. – MrSham May 19 '13 at 13:49
-
@MrA: No; not all collections inherit the abstract classes. – SLaks May 19 '13 at 13:54
-
@PatriciaShanahan I also agree on comment. But take example below `interface Vehicle { move(); reverse(); changeGear(); } abstract AbstractVehicle{ move(){ // deafult implementaion of move } } class car extends AbstractVehicle implements Vehicle{ }` it seems awkward doing this unless there is some reason. I want to know that reason.. – MrSham May 19 '13 at 14:00
-
@MrA First of all, it is important to recognize that the interface is the important external definition. The abstract base class is just used for implementation convenience. If Java supported private inheritance, List would privately inherit AbstractList. Now consider a method such as isEmpty(). If a collection has a fast size() method, it has an obvious implementation. It would be silly to duplicate that code in every collection that should use it. – Patricia Shanahan May 19 '13 at 14:12
-
@SLaks Ok not all collectin but some of them . for example public class ArrayList
extends AbstractList – MrSham May 19 '13 at 14:25implements List ....What would happen if ArrayList didnt implement List just extending AbstractList... Would You think there would be some drawback or flaws in design?
0
The Collection
interface defines the contract, and the AbstractCollection
contains the "Skeletal implementation" of the functionality.
To read more about it, I recommend understanding "design by contract" and skeletal implementations, best described in Joshua Bloch's Effective Java - episode 18.

onon15
- 3,620
- 1
- 18
- 22