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
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/Setexample:Collection, then you don’t know anything of the behaviour ofadd– 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
addmethod.