Why would we need to allow interfaces to extend only from java.lang.Object and not any other class? [duplicate]
Asked Answered
L

2

6

As per my understanding from some books on Java, interfaces cannot extend classes.

But all interfaces do inherit methods from Object class. Why is this so?

If Interface not extend from Object class. So how this code work it?

interface A
{    
 public boolean equals(Object o);

}

class InterfaceAndObjectClass
{

    public static void main(String[] args)
    {
        A a = null;

        a.equals(null);

        a.hashCode();

        a.toString();
    }
}

Please help me to explain how access the Object class method in Interface.

Lilah answered 20/11, 2015 at 11:57 Comment(0)
S
6

An interface implicitly contains all the methods of Object class. And since any class that implements that interface is a sub-class of Object, it contains the implementation of all those methods.

JLS 9.2

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless an abstract method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

Skylar answered 20/11, 2015 at 12:0 Comment(11)
so yau can say Interface not extended implicitly contains all the methods of objecs class? can you explain the implicitly contains Object class methods in interface. thanksLilah
@Lilah "implicitly contains" means that as far as the compiler is concerned, any interface that doesn't extend other interfaces declares all the methods of Object class.Skylar
Abstract classes extend Object, and you can't invoke the new operator on them. So even if interfaces extended Object (which they don't), that doesn't imply you'd be able to instantiate them.Lilah
@Lilah I don't understand what you were trying to say with your last comment, and how it is related to my answer.Skylar
@Skylar As per the JLS 9.2 you have mentioned, its no where saying that interface has parent class is object, it says that the interface will implicitly have the methods of object class, please correct me if I am wrong.Gusgusba
@Skylar how the compiler put the method in interface. Why compiler to do that activity in Interface?Lilah
And when any class implements this interface, if that class is not extending any other class then it would have Object class as parent class, but in this case won't there be two same methods coming from interface as well as object class?Gusgusba
@Lilah As the quote from the JLS says - the interface implicitly declares a public abstract member method - it puts an abstract method (i.e. no implementation).Skylar
@BilboBaggins I never said interfaces has Object as their parent class. I said that any class that implements any interface (or in other words, any implementation of an interface) is a sub-class of Object.Skylar
@Skylar I got that, and I know that super class of any Class is by default Object, I also know that the interface doesn't have Object as super class but I have this query, what if we are implementing an interface A on class AImpl, now the A interface also have the same set of methods (as in object class) and the AImpl's parent by default would be Object class it also has the same set of methods, correct?Gusgusba
@BilboBaggins AImpl will be required to implement all of A's methods (assuming AImpl is not abstract) which includes the implicitly declared abstract methods having the same signature as the methods of Object class. But since AImpl is a sub-class of Object, it already implements all those methods.Skylar
R
1

Eran is absolutely right. Actually Interface does not extend or implement Object class. It Implicitly contains all the methods of Object class. Note(further):

If you declare same (abstract) method(with same name-signature, return type, and throws type, compiler will consider it as overriding(its ok) but if you declared with different return type or different throws type compiler will will force you to keep same(compatible) return type or throws clause.

try to declare below equals method in your interface. public int equals(Object obj); //it will show compile time error return type is //not compatible. (it should be boolean not int)

Ricer answered 20/11, 2015 at 12:14 Comment(2)
why implicitly Object class method in Interface and what is the mechanism to put the methods in Interface?Lilah
As your interface will refer an object that implement your interface. And this object will inherit all instance methods of Object class so these are made available in your interface so that you can call them through your interface reference too.Ricer

© 2022 - 2024 — McMap. All rights reserved.