Do Interfaces Really Inherit the Object Class (the Cosmic Superclass)

Do Interfaces Really Inherit the Object Class (the Cosmic Superclass) In Java?

Well… the answer is NO. An interface can’t inherit from a class in Java, not at least directly. So, we can safely say that interfaces don’t inherit from the Object class. Okay… so how can they do that indirectly? We know that interfaces can have classes declared as members as well just like they can have constants. Such a class is called a member class and like constants all the member classes of an interface would be static and public. And that static member class (like any other class in java) inherits Object class.

But, how are we able to compile code having Object method calls on the references of an interface type in Java? We all know that the object of the implementing class (and hence its type) will be assigned to only at run time and we can compile a code only if the compiler finds a method of that signature in the declared type (either declared directly or inherited from superclasses). This is absolutely correct for classes in Java, but only partially correct for interfaces in Java. Surprised? Let’s try to understand what internally happens in case of interfaces.

The Java Language Specification clearly says that the members of an interface are those which are declared in the interface and those which are inherited from direct super interfaces. If an interface has no direct superinterface then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in the Object class, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by that interface. This is what makes the signatures of the Object methods available to the compiler and the code compiles without any error. Remember if the interface tries to declare a public instance method declared ‘final’ in the Object class then it’ll result into a compile-time error. For example, ‘public final Class getClass()’ is a public instance method declared ‘final’ in the Object class and therefore if an interface tries to declare a method with this signature then the compilation will fail.

Is this Inheritance of Object methods by the Interfaces?

No. This can not be termed as ‘Object methods being inherited by the Interfaces’. This is just a special treatment given to the interfaces in Java.

In this case all the qualified (public instance) Object class methods are declared as public abstract, which is not inheritance. Right? In inheritance we get the definition of the method as well and the non-abstract methods are not inherited as ‘abstract’. But an interface in Java can’t have any of these two – definition or non-abstract method. Hence, the designers of Java had to think of an alternative.

Moreover, only public instance methods are implicitly declared in the interfaces and what about other methods – for example, protected Object clone() and protected void finalize()? In case of inheritance they are also inherited by the subclasses.

Thus, we see that it’s not exactly inheritance of the Object class by the interfaces. An interface can’t inherit a class for the simple reason that interfaces can only have abstract methods (i.e., they can’t have body). Please don’t say that we can have an abstract class having only abstract methods which can be inherited safely by interfaces :-) We will better have an interface in that case.

Example: a simple Java program showing Object method access on interface ref type

package test;

public class TestInterfaceDemo{

public static void main(String[] args) {

TestInterface testInterface = new TestInterfaceImpl();

//… calling Object class method – toString – OK

System.out.println(testInterface.toString());

//… calling the interface method – testMethod – OK

testInterface.testMethod();

//… calling the implementing class method – implClassMethod – ERROR

//testInterface.implClassMethod();

//… calling the same method after casting the reference – OK

((TestInterfaceImpl)testInterface).implClassMethod();

}

}

package test;

public class TestInterfaceImpl implements TestInterface{

public void testMethod(){

System.out.println(“Test Method if the Interface”);

}

public void implClassMethod(){

System.out.println(“Test Interface Impl Class Method”);

}}

Output:-

test.TestInterfaceImpl@45a877 (variable… will probably be diff for you)Test Method if the InterfaceTest Interface Impl Class Method

If we uncomment the line ‘//testInterface.implClassMethod();’ for accessing a method of the implementing class which is not a member of the interface type then expectedly we get a compiler error:-

Error(14,23): method implClassMethod() not found in interface test.TestInterface

As at the compiler doesn’t know the type of the assigned object and hence can’t resolve the signature of the method call on the declared reference type during compilation and therefore report an error. I hope the above explanation helps.