默认情况下接口是否扩展了Object? [英] Does an interface by default extend Object?

查看:88
本文介绍了默认情况下接口是否扩展了Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您定义如下界面

interface I1{

}

您可以在任何代码部分编写

The in any code section you can write like

I1 i1;
i1.equals(null);

然后从equals方法到来的地方,接口是否也扩展了超类Object ?,如果是true接口如何扩展类?

Then from where the equals method come, is the interface also extends the super class Object ?, if that true how an interface can extends a class?

假设让接口扩展超类Object,那么如果你看到为什么像Set thave这样的集合接口定义了equals()和hashCode()方法?所有类都扩展了Object类,因此如果在Object类中的接口中定义任何抽象方法,那么实现接口的人就不需要实现这些方法。如下面的代码所示

Suppose let the interface extends the super class Object , then if you see why the collection interface like Set thave define the equals() and hashCode() method ?. All the class extends the Object class so if you define any abstract method in a interface which present in Object class then who implement the interface they no need to implements those method. Like in below code

interface I1{
String toString();
}

class A implements I1{

}

这里的类A不需要像Object类中那样实现toString()方法。
然后在集合接口中定义这些方法的目的是什么,因为它们不能强制实现类来实现这些方法。

Here the class A no need to implements the method toString() as it's present in Object class. Then what is the objective of defining those method in collection interface as they can't force there implementation class to implement those method.

推荐答案


然后从equals方法到来的地方,接口是否也扩展了超类Object ?,如果这样,接口可以如何扩展类?

Then from where the equals method come, is the interface also extends the super class Object ?, if that true how an interface can extends a class?

Java语言规范明确处理此问题。

The Java Language Specification deals with this explicitly.

来自第9.2节


如果接口没有直接的超接口,则接口隐式声明一个公共抽象成员方法m,其中包含签名s,返回类型r和throws子句t,对应于具有签名s的每个公共实例方法m,返回类型r ,和throws子句在Object中声明,除非一个方法具有相同的签名,相同的返回类型和兼容的t hrows子句由接口显式声明。

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.

基本上,这是可以使用等于 hashCode 等 - 因为指定Java语言的方式意味着接口的任何具体实现都将是一个类,并且该类必须最终是 Object 的子类,因此成员肯定会出现。

Basically, this is so that you can use equals, hashCode etc - because the way that the Java language is specified means that any concrete implementation of the interface will be a class, and that class must ultimately be a subclass of Object, so the members will definitely be present.

换句话说,虽然接口本身不扩展 Object ,但已知任何实现都会。

To put it another way, while the interface itself doesn't extend Object, it is known that any implementation will.


这里的A类不需要像Object类中那样实现toString()方法。那么在集合接口中定义那些方法的目的是什么,因为它们不能强制实现类来实现那些方法。

Here the class A no need to implements the method toString() as it's present in Object class. Then what is the objective of defining those method in collection interface as they can't force there implementation class to implement those method.

通常这只是为了清楚起见,例如记录在 Object 中声明的成员的实现期望。

Usually this is just done for clarity, e.g. to document what is expected of an implementation in terms of the members declared in Object.

这篇关于默认情况下接口是否扩展了Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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