简体   繁体   中英

Interfaces extending other interfaces but also containing same methods

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();
}

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 Queue s.

add(E)

Compare the source code, example add :

Queue :

/**
 * Inserts the specified element into this queue if it is possible to do so
 * immediately without violating capacity restrictions, returning
 * {@code true} upon success and throwing an {@code IllegalStateException}
 * if no space is currently available.
 *
 * @param e the element to add
 * @return {@code true} (as specified by {@link Collection#add})
 * @throws IllegalStateException if the element cannot be added at this
 *         time due to capacity restrictions
 * @throws ClassCastException if the class of the specified element
 *         prevents it from being added to this queue
 * @throws NullPointerException if the specified element is null and
 *         this queue does not permit null elements
 * @throws IllegalArgumentException if some property of this element
 *         prevents it from being added to this queue
 */
boolean add(E e);

Deque :

/**
 * Inserts the specified element into the queue represented by this deque
 * (in other words, at the tail of this deque) if it is possible to do so
 * immediately without violating capacity restrictions, returning
 * {@code true} upon success and throwing an
 * {@code IllegalStateException} if no space is currently available.
 * When using a capacity-restricted deque, it is generally preferable to
 * use {@link #offer(Object) offer}.
 *
 * <p>This method is equivalent to {@link #addLast}.
 *
 * @param e the element to add
 * @return {@code true} (as specified by {@link Collection#add})
 * @throws IllegalStateException if the element cannot be added at this
 *         time due to capacity restrictions
 * @throws ClassCastException if the class of the specified element
 *         prevents it from being added to this deque
 * @throws NullPointerException if the specified element is null and this
 *         deque does not permit null elements
 * @throws IllegalArgumentException if some property of the specified
 *         element prevents it from being added to this deque
 */
boolean add(E e);

The diff:

区别

(diff created by P4Merge)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM