Harsha Vardhan
Harsha Vardhan

Reputation: 49

Why we cannot implement collection interface directly by a concrete class?

Collection interface defines the most common general methods which can be applicable for any Collection object.

Some of the methods are like:

1) boolean add(Object obj)

2) boolean addAll(Collection c)

3) boolean remove(Object obj)

4) boolean removeAll(Collection c) (Removes particular group of objects.)

5) boolean retainAll(Collection c) (Removes all the elements except those present in c)

I want know the justification for this statement.

There is no concrete class which implements collection interface directly.

Upvotes: 2

Views: 2050

Answers (1)

Eran
Eran

Reputation: 393801

There is nothing stopping you from creating a concrete direct implementation of Collection. However, such an implementation would probably have some additional properties not covered by the Collection contract.

For example, if the elements of your Collection implementation have an ordering, you might as well implement the List interface.

On the other hand, if the storage of your Collection implementation doesn't allow duplicate elements, you might as well implement the Set interface.

...and so on.

This may give you an idea why no concrete direct implementation was deemed necessary by the designers of the standard Collections library.

Upvotes: 3

Related Questions