Do interfaces inherit from Object class in java

JavaInheritanceInterface

Java Problem Overview


Do interfaces inherit from Object class in Java?

If no then how we are able to call the method of object class on interface instance

public class Test {
    public static void main(String[] args) {
        Employee e = null;
        e.equals(null);
    }
}
    
interface Employee {
}

Java Solutions


Solution 1 - Java

> Do interfaces inherit from Object class in Java?

No, they don't. And there is no common "root" interface implicitly inherited by all interfaces either (as in the case with classes) for that matter.(*)

> If no then how we are able to call the method of object class on interface instance

An interface implicitly declared one method for each public method in Object. Thus the equals method is implicitly declared as a member in an interface (unless it already inherits it from a superinterface).

This is explained in detail in the Java Language Specification, § 9.2 Interface Members.

> ### 9.2 Interface Members > [...] > > - 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 a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface. > > [...]


This post has been rewritten as an article here.


(*) Note that the notion of being a subtype of is not equivalent to inherits from: Interfaces with no super interface are indeed subtypes of Object (§ 4.10.2. Subtyping among Class and Interface Types ) even though they do not inherit from Object.

Solution 2 - Java

Object is a supertype of any interface [1]

However, an interface does not implements, extends, or, "inherit from" Object.

JLS has a special clause to add Object methods into interfaces [2]

[1] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.10.2

[2] http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.2

Solution 3 - Java

There is actually a superclass field in every .class file, including those that represent interfaces.

For an interface it always points to java.lang.Object. But that isn't used for anything.

Another way to look at it is:

interface MyInterface {
    // ...
}

public myMethod(MyInterface param) {
    Object obj = (Object) param;
    // ...
}

Here the cast (Object) param is always valid, which implies that every interface type is a subtype of java.lang.Object.

Solution 4 - Java

That's because employee e = ... reads that there is a class that implements employee, and is assigned to variable e. Every class that implements an interface extends Object implicitly, hence when you do e.equals(null), the language knows that you have a class that is a subtype of employee.

The JVM will do runtime checking for your code (i.e. throw NullPointerException).

Solution 5 - Java

Is interface inherits Object class, how can we able to access the methods of object class through a interface type reference
No Interface does not inherits Object class, but it provide accessibility to all methods of Object class. The members of an interface are:

Those members declared in the interface.
Those members inherited from direct superinterfaces.
If an interface has no direct superinterfaces, then the interface implicitly 

declares a public abstract member method corresponding to each public instance method declared in Object class.
It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.

Now it is clear that all superinterface have abstract member method corresponding to each public instance method declared in Object.

source: http://ohmjavaclasses.blogspot.com/2011/11/is-intreface-inherits-object-clashow.html

Solution 6 - Java

Any class implementing any interface is also derived from Object as well by definition.

Solution 7 - Java

"Reference types all inherit from java.lang.Object. Classes, enums, arrays, and interfaces are all reference types."

Quoted from: http://docs.oracle.com/javase/tutorial/reflect/class/index.html Second sentence to be clear.

Solution 8 - Java

An interface does not and cannot extend Object class, because an interface has to have public and abstract methods.

For every public method in the Object class, there is an implicit public and abstract method in an interface.

This is the standard Java Language Specification which states like this, >“If an interface has no direct super interfaces, 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 a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.”

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpondsView Question on Stackoverflow
Solution 1 - JavaaioobeView Answer on Stackoverflow
Solution 2 - JavairreputableView Answer on Stackoverflow
Solution 3 - JavafinnwView Answer on Stackoverflow
Solution 4 - JavaBuhake SindiView Answer on Stackoverflow
Solution 5 - JavaSheoView Answer on Stackoverflow
Solution 6 - JavajabalView Answer on Stackoverflow
Solution 7 - Javadalvarezmartinez1View Answer on Stackoverflow
Solution 8 - JavasehrpenntView Answer on Stackoverflow