为什么界面中有Object类方法? [英] Why Object class methods are available in interface?

查看:98
本文介绍了为什么界面中有Object类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下接口和类已成功编译。
下面的输出中提到了问题:

interface MyInterface{}

class MyClass implements MyInterface{}

class InterDoubt{

    static MyInterface mi ;//= new MyClass() ;

    public static void main(String[] args){
        System.out.println("X") ;

        try{
            synchronized(mi){
                try{
                    mi.wait(4000) ;
                }
                catch(InterruptedException ie){
                    System.out.println("Exception occured at main.") ;
                }
            }
        }
        catch(Exception e){
            System.out.println("voilla, MyInterface is an interface,\n" + 
                       "then why compiler allows compilation of\n" +
                       "mi.getClass(), mi.wait().\n" +
                       "Or how the methods of Object class are available in an interface."
            );
        }

        System.out.println("Y") ;
    }
}

输出

X

voilla,MyInterface是一个界面,

voilla, MyInterface is an interface,

那么为什么编译器允许编译

then why compiler allows compilation of

mi.getClass(),mi.wait()。

mi.getClass(), mi.wait().

或如何在接口中使用Object类的方法。

Or how the methods of Object class are available in an interface.

Y

已编辑: -
我接受来自disown的回答,因为它是最具说明性的。但在阅读完答案之后,又填写了一个问题: -

Edited :- I am accepting answer from disown, as it's the most explanatory. But after reading the answer, one more issue get's populated :-

请记住,如果接口尝试声明在Object中声明为'final'的公共实例方法然后它会导致编译时错误。例如,'public final Class getClass()'是在Object类中声明为'final'的公共实例方法,因此如果接口尝试使用此签名声明一个方法然后编译将失败(引自解释)。

然后为什么以下代码被成功编译: -

then why the following code is getting successfully compiled :-

interface MyInter{
    public void method() ;
}

class MyClass implements MyInter{

    public final void method() {
        .......
        .......
              .......
    }

}


推荐答案

您正确指出的是Java语言规范中指定的异常。接口将自动从类java.lang.Object中添加所有成员。从此处

What you are correctly pointing out as an exception is specified in the Java Language Specification. Interfaces will automatically get all members from the class java.lang.Object added. From here:


Java语言规范明确指出接口的成员是在接口中声明的成员和从直接超级接口继承的成员。如果接口没有直接超接口,则接口隐式声明与Object类中声明的每个公共实例方法对应的公共抽象成员方法,除非具有相同签名,相同返回类型和兼容throws子句的方法由那个界面。这使得Object方法的签名可供编译器使用,代码编译时没有任何错误。请记住,如果接口尝试在Object类中声明一个声明为'final'的公共实例方法,那么它将导致编译时错误。例如,'public final Class getClass()'是在Object类中声明为'final'的公共实例方法,因此如果接口尝试使用此签名声明方法,则编译将失败。

The Java Language Specification clearly says that the members of an interface are those which are declared in the interface and those which are inherited from direct super interfaces. If an interface has no direct superinterface then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in the Object class, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by that interface. This is what makes the signatures of the Object methods available to the compiler and the code compiles without any error. Remember if the interface tries to declare a public instance method declared 'final' in the Object class then it'll result into a compile-time error. For example, 'public final Class getClass()' is a public instance method declared 'final' in the Object class and therefore if an interface tries to declare a method with this signature then the compilation will fail.

这篇关于为什么界面中有Object类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆