java - Java中的隐式 super 接口(interface)?

标签 java interface

所以我们都知道所有的类都隐式扩展了Object。接口(interface)怎么样?是否有隐式 super 接口(interface)?我说有。编译以下代码:

java.io.Serializable s1 = null;
java.io.Serializable s2 = null;

s1.equals(s2);

equals 方法不是在 Serializable 中声明的,而是在 Object 中声明的。由于接口(interface)只能扩展其他接口(interface),而 Object 是一个类,而不是接口(interface),因此必须有一些隐式接口(interface)正在被扩展。然后 Object 类必须隐式实现这个隐式接口(interface)(哇,写起来很奇怪)。

那么,问题是,这有多正确?

最佳答案

Since interfaces can only extend other interfaces, and Object is a class, not an interface, there must be some implicit interface that is being extended.

没有。引用 Java Language Specification :

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

这与您的“隐式 super 接口(interface)”之间的区别在于 Object 有许多最终和 protected 方法,您不能在接口(interface)中使用这些修饰符。

关于java - Java中的隐式 super 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/803466/

相关文章:

java - 我在我的解密方法中得到 BadPaddingException

java - 如何在Android应用程序中发现文章文字大小发生变化

java - 线程 "main"java.lang.NoClassDefFoundError : org/jsoup/nodes/Element 中出现异常

java - 如何在 hibernate postPersist 监听器中持久化实体

java - Android - 如何从 API (rottentomatoes) JSON 获取 url/链接

C# 将类型传递给要在 "is"语句中评估的方法?

java - 匿名类重写与传递接口(interface),用于在 Java 中设计回调

c++ - 定义自引用接口(interface)的正确方法是什么?

C# 和 ReSharper : Checking an object's type

java - 跨多个版本实现接口(interface)