• interface有没有继承Object


    今天看到一个问题:interface有没有继承Object?        答案是:没有

    根据经验

    interface A{
    	
    }
    
    class B implements A{
    	public String testB(){
    		return "testB() from class B";
    	}
    }
    
    public class Demo{
    	public static void main(String[] args){
    		A a = new B();
    		System.out.println(a.toString());
    		System.out.println(a.equals(null));
    		// System.out.println(a.testB()); //错误
    		System.out.println(((B)a).testB());
    	}
    	
    }

    既然接口的引用可以调用Object的方法为什么没有继承Object呢?

    以下答案来自https://bbs.csdn.net/topics/350246605好棒的回答

    参考: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。


  • 相关阅读:
    《走近心理学》第一章之行为和心理学
    《解忧杂货铺》读书笔记
    追求得到之日即其终止之时, 寻觅的过程亦即失去的过程。——村上
    简朴的生活、高贵的灵魂是人生的至高境界。——杨绛
    Laravel Seeder
    Git的使用 checkout push merge
    基于 GraphQL 构建 Laravel API —— 基本使用篇
    awk基础04-内置函数
    awk基础03-分支和循环语句
    awk基础02-变量-分隔符-数组
  • 原文地址:https://www.cnblogs.com/wei1/p/9582105.html
Copyright © 2020-2023  润新知