Learnitweb

Does an interface extends Object?

public class MyClass implements MyInterface {
     public static void main(String[] args) {
           MyInterface myInterface;
           myInterface = new MyClass();
           myInterface.equals(null);
     }
}
interface MyInterface {
}

Consider the above code. We have defined an interface and have not provided any method in it. Since MyClass implements MyInterfacemyInterface reference variable can be used to refer to a MyClass object. Thus, methods provided by interface can be invoked using variable myInterface.

We can see from above code that interface has not defined any method, then the question comes to our mind is how equals method is being invoked. equals method is available in Object class. Does it mean, interfaces extends Object class.

The answer to this question is No.

This has been explained in Java Language Specification. Following the extract from Java Language Specification:

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

However, an interface does not extends Object, but all of it’s implementations will extend Object. We can say that an interface implicitly declares one method for each public method in Object