Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 628
Z.I.J Avatar asked Nov 20 '15 11:11

Z.I.J


People also ask

Does interface extend object class by default?

Object is a class, and interfaces can not extend classes, so "no" - the interface doesn't inherit anything from any class.

Why Interfaces are preferred over abstract classes java?

Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing a common functionality to unrelated classes. Interfaces are a good choice when we think that the API will not change for a while.

What are the advantages of interface over abstract classes?

The main advantages of interface over abstract class is to overcome the occurrence of diamond problem and achieve multiple inheritance. In java there is no solution provided for diamond problem using classes. For this reason multiple inheritance is block using classes in java.

In which of these situations are interfaces better than abstract classes?

An interface is better than a abstract class when you want multiple classes to implement that interface and when you don't have to inherit default behavior.


2 Answers

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.

like image 123
Eran Avatar answered Oct 13 '22 17:10

Eran


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)

like image 25
nwzhaider Avatar answered Oct 13 '22 18:10

nwzhaider