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

Well… the answer is no. An interface cannot derive from a class in Java, at least not directly. Therefore, we can safely say that interfaces do not inherit from Object classes. OK… so how can they do this indirectly? We know that interfaces can also have classes declared as members just as they can have constants. Such class is called member class and like constants all member classes of an interface will 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 containing object method calls on references of an interface type in Java? We all know that the object of the implementing class (and hence its type) will be assigned only at run time and we can compile a code if the compiler finds a method of that signature in the declared type (either directly declared or inherited is) from superclass). This is perfect for classes in Java, but only partially true for interfaces in Java. to wonder? Let’s try to understand what happens internally in case of interfaces.

Java Language Specification explicitly states that the members of an interface are those which are declared in the interface and which are inherited from the direct super interface. If an interface has no direct superinterface, the interface explicitly declares a public abstract member method for each public instance method declared in the Object class, as long as the same signature, the same return type, and a compatible throws clause A method with is not explicitly declared. interface. This is what makes the signatures of object methods available to the compiler and the code compiles without error. Remember that if the interface object tries to declare a public instance method declared ‘final’ in the class it will result in a compile-time error. For example, ‘public final Class getClass()’ is a public instance method declared ‘final’ in Object class and hence compilation will fail if an interface tries to declare a method with this signature.

Is it inheritance of object methods by interfaces?

No. It cannot be called as ‘object methods inherited by interfaces’. This is a special treatment given to interfaces in Java.

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

Furthermore, only public instance methods are explicitly 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.

Thus, we see that it is absolutely no inheritance of Object class by interface. An interface cannot inherit a class for the general reason that interfaces can only contain abstract methods (that is, they cannot have a body). Please don’t say that we can have an abstract class containing only abstract methods that can be safely inherited by interfaces 🙂 In that case we would be better off having an interface.

Example: A simple java program showing object method access on interface ref type

package testing;

public class TestInterfaceDemo{

public static void main(String[] Argument) {

TestInterface TestInterface = new TestInterfaceImple();

//… Calling Object class method – 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 inserting the reference – OK

((TestInterfaceImpl)testInterface.implClassMethod();

,

,

package testing;

public class TestInterfaceImpl implements TestInterface {

public void test method () {

System.out.println(“test method on interface”);

,

public void implClassMethod() {

System.out.println(“Test interface Imp Class Method”);

,
,

Output:-

test.TestInterfaceImpl@45a877 (variables… will probably be different for you)
test method if interface
Test Interface Imp Class Method

If we do ‘//testInterface.implClassMethod();’ To access a method of implementing class which is not a member of interface type, we get a compiler error as expected:-

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

As the compiler does not know the type of the specified object and hence cannot resolve the signature of the method call on the reference type declared during compilation and hence reports an error. I hope the above explanation helps.

Leave a Comment

Your email address will not be published.