I came across Collection
interface, which declares add()
method as follows:
boolean add(E e);
Then I came across Set
interface, which extends Collection
, which declares add()
method with same above signature.
Since the signatures are same, I suppose Set
is overriding method in Collection
. The only difference I can notice is in comments. Collection
says:
Ensures that this collection contains the specified element.
Set
says:
Adds the specified element to this set if it is not already present.
So I feel, technically, we can omit declaration of add
from Set
and still the whole Java collections framework would have behaved without any issue, but its there simply because in Set
, add()
has different meaning than that in Collection
.
Q1. Is it so? Or anything else is achieved with overriding add()
in Set
?
Q2. Is this normal usage pattern across the framework? Does any other interface hierarchy follow the same?
Q3. How correct it is to override method from super interface in sub interface, just because it has different meaning in sub interface, even though it has same signature? (Is it that this is perfectly correct and as expected and I am unnecessarily finding it surprisingly weird because I am facing it for first time?)