2

As per Object Oriented Programming (OOP) Interface should not inherit Class.

Since Interface cannot inherit Class, then how Object class methods available to the interface reference in java?

Eg :-

public interface Test {

    public void functionA();
}
class Child implements Test{

  public void functionA() {

  }

  public static void main(String[] args) {
     Test test = new Child();
     test.toString(); // since toString is Objects class method, How it's visible for Test interface ref?
  }
}

since toString is Object's class method, How it's visible for Test interface ref?

bagrat
  • 7,158
  • 6
  • 29
  • 47
Pavan
  • 337
  • 1
  • 4
  • 10

3 Answers3

6

Interface do not inherit from Object class in Java and there is no common Object (root) interface implicitly inherited by all interfaces either (as all classes do)

An interface has one implicit method declared for each public method in Object.

Refer Java Language Specification 9.2 Interface Members.

Nitesh Virani
  • 1,688
  • 4
  • 25
  • 41
4

Every interface type is specified to implicitly declare all the public Object methods. That is how the Java Language Specification deals with this formality.

9.2. Interface Members

...

  • 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 an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.
Community
  • 1
  • 1
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

Every class you could possibly write implicitly inherits from Object.

Because interfaces (for example java.util.List) will for sure reference some object (of a class like ArrayList, LinkedList or some other implementation of List interface), Java let's you use Object's methods.

darijan
  • 9,725
  • 25
  • 38