If you define a interface like below
interface I1{
}
The in any code section you can write like
I1 i1;
i1.equals(null);
Then from where the equals method come, is the interface also extends the super class Object ?, if that true how an interface can extends a class?
Suppose let the interface extends the super class Object , then if you see why the collection interface like Set thave define the equals() and hashCode() method ?. All the class extends the Object class so if you define any abstract method in a interface which present in Object class then who implement the interface they no need to implements those method. Like in below code
interface I1{
String toString();
}
class A implements I1{
}
Here the class A no need to implements the method toString() as it’s present in Object class.
Then what is the objective of defining those method in collection interface as they can’t force there implementation class to implement those method.
The Java Language Specification deals with this explicitly.
From section 9.2:
Basically, this is so that you can use
equals,hashCodeetc – because the way that the Java language is specified means that any concrete implementation of the interface will be a class, and that class must ultimately be a subclass ofObject, so the members will definitely be present.To put it another way, while the interface itself doesn’t extend
Object, it is known that any implementation will.Usually this is just done for clarity, e.g. to document what is expected of an implementation in terms of the members declared in
Object.