user2820072 user2820072 - 11 months ago 44
Java Question

Does Interface java.util.Map overrides hashCode() and equals() from Object class

As per the Java-Doc for java.util.Map, the

methods are overridden from the
class. But interfaces do not inherit from the
class, so how does the
interface override the
class' methods?

Answer Source

The @Override annotation has special consideration for methods inherited from Object:

Quoting Java Language Specification, ยง9.6.4.4 @Override:

If a method declaration is annotated with the annotation @Override, but the method does not override or implement a method declared in a supertype, or is not override-equivalent to a public method of Object, a compile-time error occurs.

This means that @Override can annotate interface methods that will be inherited from Object in the implementing class, even though the interface itself is not inheriting from Object.

It is used as a safeguard against accidentally declaring an interface method that clashes with Object methods.