What is the architectural significance of declaring a generic method with the same declaration in the child interface

the interface List and Collection, and Set and Collection, declare various common methods. since both List and Set extends Collection, what is the importance of architecture , declaring that the generic method has the same declaration in the child interface.

public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);//some more common  method declaration
}

public interface List<E> extends Collection<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
Iterator<E> iterator();
Object[] toArray();
boolean add(E e);
boolean remove(Object o);
}

      

it didn't hit me, I just want to know about the concept behind it.

+3


source to share


1 answer


This is not an architectural reason. This is a way to "override" the documentation. For example, for the size method on List, they refer to 'list', but for the size method in Colleciton, they refer to 'collection'. Same for other methods:

Collection source and javadoc

     /**
      * Returns the number of elements in this collection.  If this collection
      * contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
      * <tt>Integer.MAX_VALUE</tt>.
      *
      * @return the number of elements in this collection
      */
     int size();

     /**
      * Returns <tt>true</tt> if this collection contains no elements.
      *
      * @return <tt>true</tt> if this collection contains no elements
      */
     boolean isEmpty();

      



List of sources and javadoc

     /**
      * Returns the number of elements in this list.  If this list contains
      * more than <tt>Integer.MAX_VALUE</tt> elements, returns
      * <tt>Integer.MAX_VALUE</tt>.
      *
      * @return the number of elements in this list
      */
     int size();

     /**
      * Returns <tt>true</tt> if this list contains no elements.
      *
      * @return <tt>true</tt> if this list contains no elements
      */
     boolean isEmpty();

      

So this is just a matter of good documentation.

+2


source







All Articles