If an interface does not extends Object class then why the interface references shows toString(), hashCode() and other Object’s method.
-
@JohanKarlsson The question seems clear enough to me - what part is not clear for you? – Duncan Jones Dec 12 '14 at 09:30
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
m
with signatures
, return typer
, and throws clauset
corresponding to each public instance methodm
with signatures
, return typer
, and throws clauset
declared inObject
, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

- 1,421,763
- 867
- 9,128
- 9,194
-
**What does this means** "If an interface has no direct super-interfaces, 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." **If I disassemble the class using javap it only shows explicit methods even I don't extend any super-interfaces?** – Salil V Nair Dec 12 '14 at 09:33
-
@SalilVNair: It's talking about the logical members of the interface, from the perspective of "What can I call on an expression of an interface type?" – Jon Skeet Dec 12 '14 at 09:47
-
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.

- 67,400
- 29
- 193
- 254
-
But the foo variable is of type SomeInterface and no other methods which are not defined in it be accessible at runtime right? So Is there any abstract methods of Objects are maintained by JVM? – Salil V Nair Dec 12 '14 at 10:31
-
@SalilVNair The foo variable points to an object of type `ConcreteImplementation`. The fact that your store a reference to it in a `SomeInterface` variable is not relevant. – Duncan Jones Dec 12 '14 at 10:40
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()
.

- 20,430
- 4
- 39
- 67