3

I came across fact that Collection interface has methods equals and hashCode which are also contained in Object. Same is the case with List interface. I was having following doubts:

  1. Why interfaces should have these methods? Is it only because they have different meaning than those in Object ?
  2. According to this question, including these methods does not enforce the implementing class to provide their implementation as these implementations are already provided and inherited from Object. So technically including them in the interfaces does not have any effect. This again underlines importance of first doubt, why interfaces need to have these methods?

    This page says:

    These methods perform computations over the object’s state, but the interface, in general, has no access to state; only the implementing class has access to this state.

    Which I feel further increases importance of first question.

  3. If we at all need them in interfaces, then why Java framework dont have super-interface containing them and have such interfaces implement this interface, just like as all classes are subclasses of Object?

1
  • This is covered in the Java Language Specification. All objects inherit from Object, so all implementors of interfaces must have those methods. Implicitly, objects of an interface type must have them. In order to call them the interface calls must compile. In order to compile they must somehow exist in the interface. You can guess the rest of the rationale. Commented Mar 22, 2020 at 20:19

1 Answer 1

6

The List interface declares equals and hashCode so that it can document extra constraints that the implementations must follow. For example, the List interface documentation requires that the equals method must consider two lists equal if the have the same items in the same order, no matter how the lists are implemented.

There is no way the compiler or runtime can enforce these requirements, so breaking them leads to runtime bugs that may be hard to find.

Sign up to request clarification or add additional context in comments.

4 Comments

So its all about documenting some more stuff specific to those classes. That is Object.equals() says "Indicates whether some other object is "equal to" this one." Whereas Collection.equals() says "Compares the specified object with this collection for equality.". But they are not actually enforcing anything, right? I mean Collection has equals(Object), not equals(Collection)
exactly, nothing stops you from implementing equals in a way that breaks the contract - you can easily break the general contract in Object and the more specific contract in List
I guess this is same as Set having same methods as Collection. This is why doc says "The specifications accompanying these declarations have been tailored to the Set interface, but they do not contain any additional stipulations." Q1. Right? Q2. But I dont really get whats the point in practice of extending interface and then redeclaring all its methods!!??
Wrong guess. Set inherits the methods from Collection. As with classes interfaces can override methods. in the case of add there is a difference in declared exceptions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.