19
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?

Wins
  • 3,420
  • 4
  • 36
  • 70

1 Answers1

19

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

Steven Schlansker
  • 37,580
  • 14
  • 81
  • 100
  • 7
    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`. – Holger Jan 19 '15 at 11:11
  • 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? – ggb667 Nov 18 '22 at 19:22