19

如果您定义如下所示的接口

interface I1{

}

在任何代码部分中,您都可以像这样编写

I1 i1;
i1.equals(null);

那么equals方法从何而来,接口是否也扩展了超类Object?如果是这样,接口如何扩展一个类?

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

interface I1{
String toString();
}

class A implements I1{

}

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

4

3 回答 3

36

那么equals方法从何而来,接口是否也扩展了超类Object?如果是这样,接口如何扩展一个类?

Java 语言规范明确地处理了这个问题。

第 9.2 节

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.

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.

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

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.

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.

于 2012-12-08T09:04:54.703 回答
3

Every class implicitly extends Object and so inherits every (non-private) method of the Object class.

Every instance has a class and therefore has all of the method of Object.

Whether an instance implements an interface or not is completely irrelevant to this point.

Object is a class, and interfaces can not extend classes, so "no" - the interface doesn't inherit anything from any class.

于 2012-12-08T09:05:05.190 回答
0

A Java Interface does not extend the java.lang.Object class but instances of objects that implement the interface extend the Object class otherwise if Java Interfaces allowed to extend the java.lang.Object class then Java would support multiple inheritance of which it does not in its language specification.

Consider the scenario below:

interface MyInterface
{ 
   // interface methods here
}
MyClass extend java.lang.Object implements MyInterface
{ 
   // override methods or interface method implementations
}

Now if MyInterface extends java.lang.Object class then that would mean the MyClass would also be extending the MyInterface interface by construction creating multiple inheritance. So makes sense that Java interfaces do not extend the java.lang.Object class as this would create chaos due to multiple inheritance.

于 2020-02-14T07:53:41.817 回答