Search code examples
javajava-8default-method

Java 8 default method interface override Object equals method


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?


Solution

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