-1

I have found in JLS this paragraph

If an interface has no direct superinterfaces, 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 a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

Does that mean that interface makes some kind of inheritance from Object API?

UPD

Why can I call Object's method via interface type?

interface I {}

I i = ...

i.toString();

I see here some kind of binding. For me it looks like we bind Object's method to I type.

JohnWinter
  • 1,003
  • 5
  • 12
  • 25

1 Answers1

2

It only means that for any variable of any interface type you can execute all of Object's methods. This makes sense, since any implementation of any interface is a sub-class of Object (either directly on indirectly), so it has implementations of all of Object's methods.

It doesn't mean that an interface "inherits" from the Object class, since interfaces can only extend other interfaces.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • If so then why I can call `Object's` method via the interface type variable (see my update)? – JohnWinter Jul 03 '15 at 14:29
  • @JohnWinter The answer to that is precisely the quote you posted from the JLS. Each interface either implicitly declares methods with the same signatures as all of Object's methods or inherits such methods from its super interfaces. – Eran Jul 03 '15 at 14:34
  • Ok, but why it is not inheritance? I don't get it. – JohnWinter Jul 03 '15 at 14:35
  • @JohnWinter I can declare a class `A` with a method `A#someMethod()` and another class `B` with a method also named `someMethod`. There's still no relation between `A` and `B`. – Sotirios Delimanolis Jul 03 '15 at 14:37