20
public interface Table<T> {

    @Overrride
    default boolean equals(Object other) {
        //do something and return true/false
    }
}

Why does the above code has compilation error of "java: default method equals in interface Table overrides a member of java.lang.Object"? Can't we override hashCode and equals method using interface default method, presumably I have methods in the same interface to determine the equality of the object implementing this interface?

0

1 Answer 1

20

No. Classes with implementations always win over default methods, so having a default hashCode or equals can never be invoked and therefore is forbidden.

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

2 Comments

Not only implementations win over default methods. Any declaration made in a non-interface class (i.e. within the superclass hierarchy) wins, even if it is declared abstract.
Agree with Holger, this is a bug. If I know I have interface methods and a reasonable toString default implementation calling those methods is possible, I should be allowed to declare on in an interface. Unsure what should happen if multiple interfaces declare default implementations, what happens now?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.