接口 到底有没有继承 Object 类

卑微的去爱你 2010-12-26 04:52:16
我们老师说接口是特殊的类 那意思是接口继承自Object咯 但是java文档没有这个结构

我很疑惑 欢迎大侠 发表意见

我查了一些资料 是说:


"接口隐含定义了一套与Object类中的方法签名完全相同的方法,所以,我们在程序中调用接口的那些与Object中具有相同签名的方法时,编译器不会报错!"
...全文
1569 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙四 2010-12-26
  • 打赏
  • 举报
回复
懒得多说,《深入java虚拟机》第二版第126页第六行


《The JavaTM Virtual Machine Specification》Second Edition CHAPTER 4 The class File Format
4.1 The ClassFile Structure中对于super_class标志的描述,我就复制一下内容吧:
super_class
For a class, the value of the super_class item either must be zero or must be a valid index into the constant_pool table. If the value of the super_class item is nonzero, the constant_pool entry at that index must be a CONSTANT_Class_info (§4.4.1) structure representing the direct superclass of the class defined by this class file. Neither the direct superclass nor any of its superclasses may be a final class.
If the value of the super_class item is zero, then this class file must represent the class Object, the only class or interface without a direct superclass.

For an interface, the value of the super_class item must always be a valid index into the constant_pool table. The constant_pool entry at that index must be a CONSTANT_Class_info structure representing the class Object.

yukiMark 2010-12-26
  • 打赏
  • 举报
回复
这个问题貌似没什么实际意义
  • 打赏
  • 举报
回复

interface HYL {
void print();
}
public class HYLTest {
public static void main(String[] args) {

}
public void add(HYL i){
get(i);
}
public void get(Object ob) {

}
}

看到上面的代码一定会想,接口继承了Object类,其实不然,但为什么get()中可以传入一个接口,应该接口可以定义引用,但JVM知道引用必然会指向一个子类对象,所有的对象都实现了Object类,子类对象也不例外,所以i肯定指向的是一个实现了这个接口的对象当然继承了Object ,,,个人见解(接口没有实现)
还可以这样分析:接口里面所有的方法都是抽象的未实现的方法,object里面的方法都实现了,如果接口可以继承Objct那么接口中就有非抽象的方法了,如果lz认为可以这样的话。。。我的答案是:不能继承
hothothml 2010-12-26
  • 打赏
  • 举报
回复
java中的任何类都会继承Object类的
genguyige 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 ticmy 的回复:]

接口继承了Object类,在.class文件中的super_class标志中可以看到,另外《深入java虚拟机》第二版以及《The Java Virtual Machine Specification》second edition中有说到


另外,通过代码也可以验证,你把一个对象赋值给一个接口的引用,是可以调用Object中的wait,notify等方法的
[/Quote]

把一个对象赋值给一个接口的引用

UPUP
龙四 2010-12-26
  • 打赏
  • 举报
回复
接口继承了Object类,在.class文件中的super_class标志中可以看到,另外《深入java虚拟机》第二版以及《The Java Virtual Machine Specification》second edition中有说到


另外,通过代码也可以验证,你把一个对象赋值给一个接口的引用,是可以调用Object中的wait,notify等方法的
若鱼1919 2010-12-26
  • 打赏
  • 举报
回复
还有一点:

接口没有构造函数,而类都有构造函数,包括抽象类。
Kisho 2010-12-26
  • 打赏
  • 举报
回复
在java中接口是一种数据类型
Kisho 2010-12-26
  • 打赏
  • 举报
回复
没有继承的啊
fengke01242010 2010-12-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 py330316117 的回复:]
接口是抽象方法和常量值的定义的集合。是一种特殊的抽象类,只包含常量和方法的定义,而没有变量和方法的实现。
而object类是所有类的父类,你知道这个就可以了,至于接口和object类有什么关系,我只能说没有什么关系。
[/Quote]
说的很好支持
若鱼1919 2010-12-26
  • 打赏
  • 举报
回复
参考:java语言规范 9.2 Interface Members

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.

如果一个接口没有直接的父接口,那么,这个接口就隐含的声明了这样的一些成员方法m,这个方法是public abstract的,签名是s,返回类型是r,抛出t类型异常,与Object类中声明的public的,签名是s,返回类型是r,抛出t类型异常的方法相对应,除非,接口中明确定义了这样的方法。
如果接口明确的声明了这样的方法m,m在Object类中是final的,这将会产生编译时error。

Object的public方法:

public final native Class<?> getClass();
public final native void notify();
public final native void notifyAll();
public final void wait() throws InterruptedException
public boolean equals(Object obj)
public native int hashCode();
public String toString()
protected native Object clone() throws CloneNotSupportedException;
protected void finalize() throws Throwable { }

也就是说,接口里面默认有equals、hashcode、toString、clone和finalize这5个方法。

做了个实验,挺有意思:

在eclipse里面:
public interface MyInterfsace {
public void toString();//compile error! The return type is incompatible with Object.toString()
public void equals(Object obj);//compile error!The return type is incompatible with Object.equals(Object)
public String finalize() throws Throwable;//compile OK!but:The return type is incompatible with Object.finalize(), thus this interface cannot be implemented
public String hashCode();//compile error! The return type is incompatible with Object.hashCode()
public String clone() throws CloneNotSupportedException;//没啥特别的反应。
}


It follows that is a compile-time error if the interface declares a method with a signature that is override-equivalent (§8.4.2) to a public method of Object, but has a different return type or incompatible throws clause.
如果接口声明了一个方法,跟Object的方法构成override,但是却有不一样的返回类型或者是不兼容的异常类型,也是编译时error。
xinwei666666 2010-12-26
  • 打赏
  • 举报
回复
我也不清楚~~~~~
JamesShoal 2010-12-26
  • 打赏
  • 举报
回复
Java中,接口可以看成是一种“纯”的抽象类。如果通过Jjavap反汇编接口生成的.class文件,查看一下它的常量池,可以看到接口也是用class这个关键字来标识的,JVM并没有为接口定义一个专门的关键字。由此可以推断,JVM并不把接口看成是与普通的类完全不同的另一种“东西”。

上摘自 数字世界-凡人 对网友的回复:http://blog.csdn.net/bitfan/archive/2010/12/18/6083935.aspx
huyong479072052 2010-12-26
  • 打赏
  • 举报
回复
应该不是继承
py330316117 2010-12-26
  • 打赏
  • 举报
回复
接口是抽象方法和常量值的定义的集合。是一种特殊的抽象类,只包含常量和方法的定义,而没有变量和方法的实现。
而object类是所有类的父类,你知道这个就可以了,至于接口和object类有什么关系,我只能说没有什么关系。
若鱼1919 2010-12-26
  • 打赏
  • 举报
回复
接口是接口,类是类。不是一个妈生的就不是一回事。

"接口隐含定义了一套与Object类中的方法签名完全相同的方法,所以,我们在程序中调用接口的那些与Object中具有相同签名的方法时,编译器不会报错!"

确实有这回事,记不清在哪看过了
pywepe 2010-12-26
  • 打赏
  • 举报
回复

interface I {
}
class O implements I{

}

//实际上是
class O extends Object implements I{

}

62,636

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧