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 not directly. So we can safely say that interfaces do not inherit from the Object class. Well… so how can they do that indirectly? We know that interfaces can have classes declared as members, just as they can have constants. Such a class is called a member class, and as 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 that has Object method calls on the references of an interface type in Java? We all know that the object of the implementing class (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 directly declared or inherited ). of superclasses). This is absolutely correct for Java classes, but only partially correct for Java interfaces. Surprised? Let’s try to understand what happens internally in the case of interfaces.

Tea Java language specification it clearly says that the members of an interface are those that are declared in the interface and those that are inherited from direct superinterfaces. If an interface does not have a direct superinterface, 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, return type is explicitly declared. and a compatible throws clause. 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’ on the Object class, a compile-time error will occur. For example, ‘public final Class getClass()’ is a public instance method declared ‘final’ on the Object class, and so if an interface tries to declare a method with this signature, the build will fail.

Is this inheritance of object methods by interfaces?

No. This cannot be called ‘Object Methods Inherited by Interfaces’. This is just a special treatment given to interfaces in Java.

In this case, all qualified methods (public instance) of the Object class are declared as public abstract, which is not inheritance. Right? In inheritance we also get the method definition 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, eg protected Object clone() and protected void finalize()? In case of inheritance they are also inherited by the subclasses.

Thus, we see that it is not exactly 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 a body). Please don’t say we can have an abstract class that has only abstract methods that interfaces can safely inherit 🙂 We’d better have an interface in that case.

Example: a simple java program that demonstrates object method access on the interface reference type

trial pack;

public class TestInterfaceDemo{

public static void main(String[] arguments) {

TestInterface testInterface = new TestInterfaceImpl();

//… calling the method of the Object class – 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();

}

}

trial pack;

public class TestInterfaceImpl implements TestInterface{

public void test() method {

System.out.println(“Test method if interface”);

}

public void implClassMethod(){

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

}
}

Production:-

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

If we uncomment the line ‘//testInterface.implClassMethod();’ to access a method of the implementing 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 on interface test.TestInterface

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

Leave a Reply

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