How can an Interface override a method of Object class? Why 'Collection' Interface Overrides equals(Object o) method and hashCode() method of Object class?
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#hashCode()
To Override a method of a certain class we need to extend that class(i.e the concept of inheritance comes into picture),Does that mean Interfaces in java extends(i.e inherits) Object class?
My understanding :
As we know an Interface in java doesnot extends Object class however for every public method in Object class, there is an implicit abstract and public method declared in every interface which does not have direct super interfaces. This is the standard Java Language Specification which states like this,
"If an interface has no direct superinterface types, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface."
(Reference : https://docs.oracle.com/javase/specs/jls/se17/html/jls-9.html)
But the 'Collection' interface has a Super Interface(i.e Iterable) then how it can override the Object class method ? as it contradicts the above definition.
Is there any difference between 'direct SuperInterface an Interface' vs 'SuperInterface of an Interface'? because the above definition mentions the word 'direct Super Interface' while Iterable is the SuperInterface of Collection.
(Reference : https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html )
Edit:
Thanks a lot!@user16320675.However it would like to know What's the purpose of overriding(i.e declaring) the Object class methods by Interfaces anyways the class implementing that interface would implicitily extend the Object class thus that class can override the required Object class methods?
In short: direct superinterface is the one that the interface extends (the parent); superinterfaces include the direct one and any superinterface of that one (parent, grandparent, grand-grand-parent, ...)
The terms superinterface and direct superinterface are defined in the Java Language Specification 9.1.3. Superinterfaces and Subinterfaces:
If an
extends
clause is provided, ...
The specified interface types are the direct superinterface types of the interface being declared.
...
One interface is a direct superinterface of another interface if the first interface is named by one of the direct superinterface types of the second interface.The superinterface relationship is the transitive closure of the direct superinterface relationship. An interface
I
is a superinterface of interfaceK
if either of the following is true:
I
is a direct superinterface ofK
.- Where
J
is a direct superinterface ofK
,I
is a superinterface ofJ
, applying this definition recursively.
Iterable
, how can it override the Object class method (e.g. equals()
)As posted in question, JLS 9.2. Interface Members states:
- If an interface has no direct superinterface types, then the interface implicitly declares a
public abstract
member methodm
with signatures
, return typer
, and throws clauset
corresponding to each public instance methodm
with signatures
, return typer
, and throws clauset
declared inObject
...
The previous point in the JLS explains:
- Members inherited from any direct superinterface types (§9.1.3).
Since Iterable
is the direct superinterface of Collection
, Collection
will inherit all members, including methods, from Iterable
. And Iterable
has no direct superinterface, so the first point applies to it and it will declare the methods of Object
.