Does the Java Collections library misuse interface inheritance? [duplicate]
There are some aspects of the Java Collections library that seem like they might be considered an abuse of interface inheritance* (subtyping) by current standards. For example, the Java Collection interface includes methods such as contains(E), which can be used to check if an item appears anywhere in the collection. This method is then required for all subinterfaces, such as Stack, even though the stack ADT doesn't generally offer such functionality. Another issue is that Deque is a subinterface of Queue. In addition to Deque having its own methods addFirst(E), addLast(E), etc., it has Queue's add(E) method. Are these uses of subtyping exemplary or problematic? Update I am not asking why collections have optional methods. None of the methods I mention are optional for the classes I discuss. I am asking if these are problematic leaky abstractions. *Josh Bloch uses the term "interface inheritance" in Effective Java (item 18) as distinguished from code inheritance.
![Does the Java Collections library misuse interface inheritance? [duplicate]](https://cdn.sstatic.net/Sites/softwareengineering/Img/apple-touch-icon@2.png?v=1ef7363febba)
There are some aspects of the Java Collections library that seem like they might be considered an abuse of interface inheritance* (subtyping) by current standards.
For example, the Java Collection
interface includes methods such as contains(E)
, which can be used to check if an item appears anywhere in the collection. This method is then required for all subinterfaces, such as Stack
, even though the stack ADT doesn't generally offer such functionality.
Another issue is that Deque
is a subinterface of Queue
. In addition to Deque
having its own methods addFirst(E)
, addLast(E)
, etc., it has Queue
's add(E)
method.
Are these uses of subtyping exemplary or problematic?
Update I am not asking why collections have optional methods. None of the methods I mention are optional for the classes I discuss. I am asking if these are problematic leaky abstractions.
*Josh Bloch uses the term "interface inheritance" in Effective Java (item 18) as distinguished from code inheritance.