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

查看:107
本文介绍了接口是否继承自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.(*)

如果没有,那么我们如何在界面实例上调用对象类的方法

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

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

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.


9.2接口成员



[...]

9.2 Interface Members

[...]


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

[...]






此帖已被重写为此处的文章。

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

(*) 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天全站免登陆