java中的接口是否继承自Object类 [英] Do interfaces inherit from Object class in java

查看:54
本文介绍了java中的接口是否继承自Object类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接口是否继承自Java中的Object类?

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 中的 Object 类?

不,他们没有.并且没有共同的根"接口被所有接口隐式继承(如类的情况).(*)

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

如果没有那么我们如何能够在接口实例上调用对象类的方法

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

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

Java 语言规范中对此进行了详细说明,§9.2 接口成员.

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

[...]

  • 如果一个接口没有直接的超接口,则该接口隐式声明一个公共抽象成员方法m,带有签名s,返回类型r 和 throws 子句 t 对应每个带有签名的公共实例方法 m s,返回类型 r 和 throws 子句 tObject 中声明,除非接口显式声明了具有相同签名、相同返回类型和兼容 throws 子句的方法.
  • 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.

[...]


这篇文章已被改写为文章此处.

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

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

这篇关于java中的接口是否继承自Object类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆