Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

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

0
Posted

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

0

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 interface

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.