Business

Do interfaces really inherit the object class (the cosmic superclass) in Java?

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

But how can we compile code with calls to the Object method on the references of an interface type in Java? We all know that the implementation class object (and therefore its type) will be assigned only at runtime 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 happens internally in the case of interfaces.

Tea Java language specification clearly states that the members of an interface are those declared in the interface and inherited from direct superinterfaces. If an interface does not have a 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, the same return type, and a throws compatible clause is explicitly declared by that interface. This is what makes the Object method signatures available to the compiler and the code compiles without any errors. Remember that if the interface tries to declare a public instance method declared ‘final’ in the Object class, it will result in 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, the compilation will fail.

Is this the inheritance of object methods by interfaces?

No. This cannot be called “object methods inherited by interfaces”. This is just a special treatment for interfaces in Java.

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

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

Therefore, we see that it is not exactly an inheritance of the Object class by the interfaces. An interface cannot inherit a class for the simple reason that interfaces can only have abstract methods (that is, they cannot have bodies). Please don’t say that we can have an abstract class that only has abstract methods that can be safely inherited by interfaces 🙂 In that case, we’d better have an interface.

Example: a simple Java program that shows access to the Object method in the interface reference type

test package;

public class TestInterfaceDemo {

public static void main (String[] arguments) {

TestInterface testInterface = new TestInterfaceImpl ();

// … calling the class method Object – toString – OK

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

// … calling interface method – testMethod – OK

testInterface.testMethod ();

// … calling implementation class method – implClassMethod – ERROR

//testInterface.implClassMethod ();

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

((TestInterfaceImpl) testInterface) .implClassMethod ();

}

}

test package;

Public class TestInterfaceImpl implements TestInterface {

public void testMethod () {

System.out.println (“Interface test method”);

}

public void implClassMethod () {

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

}

}

Production: –

test.TestInterfaceImpl@45a877 (variable … will probably be different for you)

Test method if interface

Test interface Impl class method

If we uncomment the line ‘//testInterface.implClassMethod ();’ To access a method of the implementation class that is not a member of the interface type, we would expect to get a compiler error: –

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

Since the compiler does not know the assigned object type and therefore cannot resolve the signature of the method call to the reference type declared during compilation and therefore reports an error. Hope the above explanation helps.

Leave a Reply

Your email address will not be published. Required fields are marked *