My question is
Interface Set has method add(E e)
and it extends interface Collection
.
Interface Collection
also has method add(E e)
So why do we need the same method in interface Set , since it already extends interface Collection
.
What is the purpose ?
I am stuck with that
Each of the six core collection interfaces — Collection, Set, List, Map, SortedSet, and SortedMap — has one static factory method.
The root or top-level interface of the Collection Framework is java. util. Collection. It contains some important methods such as add(), size(), remove(), clear(), iterator() that every Collection class must implement.
Since the two correct answers didn't manage to convince you, I'll try to explain.
Interfaces define contracts - i.e. they define what implementors are going (and bound) to do, so that you know that whenever you refer to an object by an interface, it has a strictly defined behaviour, no matter how exactly it has been implemented.
Now, this contract comes in two parts:
And here comes the concrete Collection
/ Set
example:
Collection
, then you don't know anything of the behaviour of add
- whether it allows duplicates or notSet
, then you are certain that no duplicates are allowed.This distinction is made by the javadoc of the redefined add
method.
Set.add
refines the contract of Collection.add
. From the latter's Javadoc:
Collections that support this operation may place limitations on what elements may be added to this collection. In particular, some collections will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. Collection classes should clearly specify in their documentation any restrictions on what elements may be added.
That's what is done in the Javadoc of Set.add
, where it states that e.g. duplicate elements are not added to the set.
(including and extending my comments below to round out this answer.)
The contract of a method specifies - formally or informally - what the caller is expected to provide as input for that method, and what is the guaranteed outcome of the method call. E.g. the contract may state that no null
parameters are expected, and if the method is passed a null
parameter, it will throw a NullPointerException
. The Javadoc of methods in the Collection framework are good examples of such contracts.
Note that some languages allow or even require the formal definition of contracts, thus the contracts are compiled into the code and actively enforced runtime. Eiffel is such a language. However, Java has no such facility; the contracts defined in Javadoc are not formal, there is not even a strict format defined for them. These are meant only for human reading, left unnoticed by the JVM. Thus, breaking a contract in Java may not be immediately noticeable, only later when the resulting bugs start to appear.
Contracts can be defined both for concrete class methods and abstract/interface methods. The contract of an interface is (should be) binding to all of its implementations. I.e. HashSet.add
, TreeSet.add
, LinkedHashSet.add
etc. all must fulfill the contract of Set.add
(and may further refine it). An implementation which does not behave according to the contract of Set.add
breaks the Liskov substitution principle.
As far as I know extended classes should have the same methods as specified by the superclass. As for the differences between the add methods I suggest you compare the javadocs for the respective add()
s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With