java 判断是否继承接口_Java 中的接口是否继承 Object 类

Java 中的 Object 类——层次结构的根,Java 中所有的类从根本上都继承自这个类。Object 类是 Java 中唯一没有父类的类。其他所有的类,包括标准容器类,比如数组,都继承了Object 类中的方法。

Java 中的接口——抽象类的变体,可以说也是一种“类”,在接口中,所有方法都是抽象的。

根据以上观点,再结合三段论的方法,可以得出——Java 中的接口也是继承 Object 类的。因为 Java 的接口也是一种“类”,所以它是继承 Object 类的。但是事实并非如此。

先来分析几段代码。

代码 1

public interfaceTestObject {voidtest();

String getString();

}

public static void main(String[] args){

Set result = new HashSet();for (Method m : TestObject.class.getMethods()) {

result.add(m.getName());

}

System.out.println(result);

}

代码的输出结果是:[test, getString],没有包括 Object 中的任何方法。

代码 2

public interfaceI { }

public static voidmain(String[] args) {

I i= null;

i.equals(null);

}

编译通过,但是运行肯定报错(空指针)。

为什么 I 接口中没有 equals 方法还可以调用?要回答这个问题,用代码是解决不了的,因为谁说的都有道理(代码 1 说明没有继承 Object 类,代码 2 说明继承了 Object 类)。

要回答此问题,就要从 Java 的标准——Java Language Specification 中找答案。在 9.2 节中 http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.2

9.2. Interface Members

The members of an interface are:

Those members declared in the interface.

Those members inherited from direct superinterfaces.

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

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.

The interface inherits, from the interfaces it extends, all members of those interfaces, except for (a) fields, classes, and interfaces that it hides and (b) methods that it overrides (§9.4.1).

Fields, methods, and member types of an interface type may have the same name, since they are used in different contexts and are disambiguated by different lookup procedures (§6.5). However, this is discouraged as a matter of style.

在 Java 的 Object 类中有三类可见方法:non-final public,final,protected。

1. final 方法,即 wait,notify,getclass,不能由一个类实现(覆盖)这些方法。因此,编译器必须防止代码的接口中声明的方法(Cannot override the final method from Object)。

2. non-final public 方法,即 equals, toString等,只要符合正确的重写规则,是可以重写的。但是实现类可以不实现它,因为它在 Object 中实现了(默认)。

3. protected 方法,即clone, finalize,如果声明这些方法在接口中,那么它们将成为公共的方法。

注意:以上接口重写 Object 类中的方法,必须要符合 Java 的方法,重写机制,要不然就是一个新方法。

总结:接口没有实现 Object 类。

最后一段代码

public interface TestObject {

// 自己接口的方法.

void test();

// 访问级别比 Object 中 hashCode 低.

//Illegal modifier for the interface method hashCode; only public & abstract are permitted

//protected int hashCode();

// 可以重写 Object 中 public 方法,但是实现类,可以不实现它.

@Override

public String toString();

//Cannot override the final method from Object

// final 的方法不能重写,编译器会报错.

//public void notify();

// protected 方法可以被提高访问级别, 实现类也必须实现.

void finalize() throws Throwable;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值