{:soso_e140:}很可惜都是答非所问。。 你们都答成子类与Object的关系了,我是问的接口。。
接口的定义有自己的一套标准,虽然编译出来也是字节码文件。。接口中不能有非抽象方法,所以肯定和Object类没有继承关系,但是能通过接口引用来调用子类对象中继承的Object类的方法,所以应该有其他形式的关系。
以下收搜索出来的解释:
去看Sun的官方文档TJLS(The Java Language Specification)吧!其中第9章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. 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.
大概意思是接口隐含定义了一套与Object类中的方法签名完全相同的方法,所以,我们在程序中调用接口的那些与Object中具有相同签名的方法时,编译器不会报错!
看来暂时只能这么理解了,也看到有说interface是一种特殊类,不继承自Object,但是编译器在编译时期做了手脚把 接口类型引用 处理为 子类对象引用。。看来想探究深层一点,要学点编译器等一类底层知识了。 |