Home > Net >  Interfaces extending other interfaces but also containing same methods
Interfaces extending other interfaces but also containing same methods

Time:08-28

Just looking Java Collections and I see the List interface, which extends the Collection interface, also contains the exact same method signatures in the Collection interface -- but not all of them just a subset. My question is why? Since it extends it, we know it will inherit them all by default. And why a subset?

The same can be seen with the Deque extending the Queue, containing all the method signatures of the Queue. Doesn't this make it harder to know what's unique about that interface?

Here is a simple example of the above:

public interface A {
   void someMethod();
}
public interface B extends A {
   void someMethod(); // why make this explicit?
   void anotherMethod();
}

CodePudding user response:

General

In general there is no reason to write out the methods you already get from the other interface again. Do not do that.

Javadoc

In case of Deque however, it is to override the Javadoc, giving more details that are specific to Deque and not to general Queues.

add(E)

Compare the source code, example add:

difference

(diff created by P4Merge)

So when you use Deques add method, you will see another Javadoc than when just using the add method of another Queue.

  • Related