169

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 {
}
aioobe
  • 413,195
  • 112
  • 811
  • 826
ponds
  • 2,017
  • 4
  • 16
  • 11
  • @EJP, technically speaking it doesn't matter what java/io/Serializable.class contains. I think you're confusing the Java Lang Spec with the JVM spec. – aioobe Jun 20 '12 at 08:13
  • @aioobe As I haven't mentioned either of those specifications I don't understand your point. `Serializable` is an interface, the simplest possible; running `javap` on it tells you what it inherits from; and that is dictated by the Java Language Specification. If you think the JVM Spec comes into it somewhere please enlighten us. – user207421 Jun 21 '12 at 08:49
  • 2
    @EJP, the question is about the Java language (i.e. the Java Language Specification). What ever java/io/Serializable.class contains is related to what the JVM spec says. Technically speaking there is no guarantee that there is a one-to-one correspondence between features of the two specifications. – aioobe Jan 04 '13 at 13:29
  • 1
    I elaborated on this in a recent [blog post](http://aioo.be/2016/08/24/JLS-vs-JVMS.html). – aioobe Sep 16 '16 at 11:28

9 Answers9

169

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.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Does this explain the behavior of `boolean isObject = myList instanceof Object` being true? Or is that because `instanceof` looks at what the object actually is, not what your reference to it is. – corsiKa Jul 12 '13 at 20:04
  • `instanceof` performs the check against the runtime type (otherwise the whole instanceof-expression could be evaluated at compile time). – aioobe Jul 12 '13 at 21:09
  • 1
    @aioobe If we implement any interface, then why don't we give the implementation of "equals" method in the class which is implementing that interface. According to my concepts,we have to implement the methods of interface in implementing class otherwise the class will be abstract. – Vikas Mangal Jul 02 '14 at 07:38
  • 1
    You don't need to (re)implement inherited methods. Have a look at [this example](http://pastebin.com/p1z6NBUx). In other words, equals is already defined and inherited to the class implementing the interface. – aioobe Jul 02 '14 at 08:34
  • After reading this answer, it makes more sense to see `class Object` as `class Root` in java. – overexchange Dec 09 '14 at 16:56
  • 3
    I got the point here. But one question- why do we need this? What difference it would have made if the methods of `Object` class would not have been declared in the interface ? – Vikas Mangal Dec 11 '14 at 18:07
  • 2
    If we didn't have this, the program in the question would not compile. There is on `equals` method in the `Employee` interface. – aioobe Dec 11 '14 at 18:44
  • below Oracle link states that, "Reference types all inherit from java.lang.Object. Classes, enums, arrays, and interfaces are all reference types." This is contradicting. Can someone throw light on it ? [Lesson: Classes](http://docs.oracle.com/javase/tutorial/reflect/class/index.html) @aioobe – Dhanaraj Durairaj Sep 25 '15 at 09:16
  • 1
    This question and answer still reminds me that even after experience i should focus on making my basics strong. – Anand Kadhi Dec 04 '15 at 07:20
  • What happens in the following case : Class Dummy extends Test implements Employee {...} . Dummy would have two methods with same signature (one defined in Object class and the other declared in interface Employee) ? – prvn Sep 16 '16 at 10:46
  • 1
    @prvn, that's not a problem since there's still just one *implementation* of the method. – aioobe Sep 16 '16 at 11:17
  • Why do interfaces need this special treatment? Wouldn't it have been simpler to inherit from Object like everything else? – jaco0646 Jul 23 '20 at 14:22
20

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

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • 1
    That's the most accurate answer. Should be the accepted one. E.g. a method which takes a ```java.lang.Object``` will also accept a reference of any interface type. Moreover you can cast interface to an ```Object``` implicitly without any compiler error. – nme Feb 10 '19 at 14:03
12

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.

finnw
  • 47,861
  • 24
  • 143
  • 221
  • 5
    The .class file is an artifact of the .java file. To argue why something works in Java language by looking at the resulting .class file is backward reasoning. – aioobe Dec 09 '14 at 17:12
  • Object obj = (Object) param; does not throw compilation error. But MyInterface's methods (public) are not visible to obj. Therefore can't assume MyInterface is every interface type is a subtype of java.lang.Object – sabarinathan u May 13 '19 at 09:02
5

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).

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
3

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

gyanu
  • 945
  • 1
  • 8
  • 20
Sheo
  • 1,034
  • 12
  • 24
0

"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.

dalvarezmartinez1
  • 1,385
  • 1
  • 17
  • 26
  • `Classes, enums, and arrays (which all inherit from java.lang.Object) as well as interfaces are all reference types` : it does not say interface inherits from Object. Only Classes , enums and arrays. – Number945 Apr 06 '18 at 07:30
  • They changed it :) – dalvarezmartinez1 Apr 11 '18 at 09:41
  • Even if "they changed it" (which I doubt), the tutorial can be wrong. The normative reference is the Java Language Specification (JLS). – Lew Bloch Mar 22 '20 at 20:36
0

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

jabal
  • 11,987
  • 12
  • 51
  • 99
0

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.”

hamal
  • 55
  • 6
0

The method of compiling the inserted object declaration is not found in the class file. The method name of the object for which the metaclass information is not obtained is reflected at runtime. According to the official description, the method signature of the object is implicitly extended, which may be allowed by the JVM runtime mechanism