If an interface does not extends Object class then why the interface references shows toString(), hashCode() and other Object’s method.
3 Answers
Because that's the way the language is designed. Any class implementing the interface will definitely have Object as an ultimate ancestor, so at execution time, those methods will definitely be available.
This is specified in JLS 9.2:
If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method
mwith signatures, return typer, and throws clausetcorresponding to each public instance methodmwith signatures, return typer, and throws clausetdeclared inObject, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
3 Comments
In Java, everything is a subclass of Object, even if it's not explicitly declared to be so. So you declare your interface, and then whatever class implements your interface must be a subclass of Object, because everything is.
And because it's a subclass of Object, it pulls in all the visible methods of Object, like .toString().
Comments
Any implementation of an interface will necessarily extend Object. For example:
SomeInterface foo = new ConcreteImplementation(); Here, ConcreteImplementation must extend Object, because it is the ultimate ancestor of all Java objects. Thus you can access all the public methods associated with the Object class through your foo variable.
2 Comments
ConcreteImplementation. The fact that your store a reference to it in a SomeInterface variable is not relevant.