接口是否继承自java中的对象类

时间:2022-03-12 07:50:06

Do interfaces inherit from Object class in Java?

接口是否继承了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 {
}

7 个解决方案

#1


130  

Do interfaces inherit from Object class in Java?

接口是否继承了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).

接口隐式地为对象中的每个公共方法声明一个方法。因此,equals方法被隐式声明为接口中的成员(除非它已经从超接口继承了它)。

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

这是详细解释Java语言规范,§9.2接口成员。

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.
  • 如果一个接口没有直接超接口,然后隐式声明了一个公共的接口抽象成员方法m签名年代,返回类型r,抛出条款t对应于每个公共实例方法m签名年代,返回类型r,并抛出对象中声明的条款t,除非相同的方法签名,相同的返回类型,一个兼容的条款是显式声明的接口。

[...]

[…]


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.

(*)请注意,作为一个亚型的概念并不等同于继承自:接口没有超级接口确实是子类型的对象(§4.10.2。类和接口类型之间的子类型)即使它们不从对象继承。

#2


9  

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

实际上,在每个.class文件中都有一个超类字段,包括那些表示接口的。

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

对于接口,它总是指向java.lang.Object。但这并不是用来做任何事情的。

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.

在这里,cast (Object) param始终有效,这意味着每个接口类型都是java.lang.Object的子类型。

#3


8  

Object is a supertype of any interface [1]

对象是任何接口[1]的超类型

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

但是,接口不实现、扩展或“继承自”对象。

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

JLS有一个特殊的子句,可以将对象方法添加到接口中[2]

[1] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.10.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

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

#4


4  

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.

那是因为雇员e =…读取有一个实现employee的类,并将其分配给变量e。每个实现接口的类都隐式扩展对象,因此当您执行e.equals(null)时,语言就知道您有一个属于employee的子类型的类。

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

JVM将对代码进行运行时检查(例如抛出NullPointerException)。

#5


2  

Is intreface 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:

是intreface继承的对象类,如何通过接口类型引用访问对象类的方法接口的成员是:

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.

声明与对象类中声明的每个公共实例方法对应的公共抽象成员方法。如果接口在将m声明为final的情况下显式地声明这样一个方法m,则会出现编译时错误。

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-*ow.html

现在很明显,所有超接口都具有与在对象中声明的每个公共实例方法对应的抽象成员方法。来源:http://ohmjavaclasses.blogspot.com/2011/11/is-intreface-inherits-object-*ow.html

#6


0  

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

实现任何接口的任何类也可以通过定义从对象派生。

#7


0  

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

引用类型都继承自java.lang.Object。类、枚举、数组和接口都是引用类型。

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

引用自:http://docs.oracle.com/javase/tutorial/reflect/class/index.html第二句。

#1


130  

Do interfaces inherit from Object class in Java?

接口是否继承了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).

接口隐式地为对象中的每个公共方法声明一个方法。因此,equals方法被隐式声明为接口中的成员(除非它已经从超接口继承了它)。

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

这是详细解释Java语言规范,§9.2接口成员。

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.
  • 如果一个接口没有直接超接口,然后隐式声明了一个公共的接口抽象成员方法m签名年代,返回类型r,抛出条款t对应于每个公共实例方法m签名年代,返回类型r,并抛出对象中声明的条款t,除非相同的方法签名,相同的返回类型,一个兼容的条款是显式声明的接口。

[...]

[…]


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.

(*)请注意,作为一个亚型的概念并不等同于继承自:接口没有超级接口确实是子类型的对象(§4.10.2。类和接口类型之间的子类型)即使它们不从对象继承。

#2


9  

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

实际上,在每个.class文件中都有一个超类字段,包括那些表示接口的。

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

对于接口,它总是指向java.lang.Object。但这并不是用来做任何事情的。

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.

在这里,cast (Object) param始终有效,这意味着每个接口类型都是java.lang.Object的子类型。

#3


8  

Object is a supertype of any interface [1]

对象是任何接口[1]的超类型

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

但是,接口不实现、扩展或“继承自”对象。

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

JLS有一个特殊的子句,可以将对象方法添加到接口中[2]

[1] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.10.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

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

#4


4  

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.

那是因为雇员e =…读取有一个实现employee的类,并将其分配给变量e。每个实现接口的类都隐式扩展对象,因此当您执行e.equals(null)时,语言就知道您有一个属于employee的子类型的类。

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

JVM将对代码进行运行时检查(例如抛出NullPointerException)。

#5


2  

Is intreface 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:

是intreface继承的对象类,如何通过接口类型引用访问对象类的方法接口的成员是:

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.

声明与对象类中声明的每个公共实例方法对应的公共抽象成员方法。如果接口在将m声明为final的情况下显式地声明这样一个方法m,则会出现编译时错误。

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-*ow.html

现在很明显,所有超接口都具有与在对象中声明的每个公共实例方法对应的抽象成员方法。来源:http://ohmjavaclasses.blogspot.com/2011/11/is-intreface-inherits-object-*ow.html

#6


0  

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

实现任何接口的任何类也可以通过定义从对象派生。

#7


0  

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

引用类型都继承自java.lang.Object。类、枚举、数组和接口都是引用类型。

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

引用自:http://docs.oracle.com/javase/tutorial/reflect/class/index.html第二句。