Java 中的接口是否繼承 Object 類


    Java 中的 Object 類——層次結構的根,Java 中所有的類從根本上都繼承自這個類。Object 類是 Java 中唯一沒有父類的類。其他所有的類,包括標准容器類,比如數組,都繼承了Object 類中的方法。

    Java 中的接口——抽象類的變體,可以說也是一種“類”,在接口中,所有方法都是抽象的。

    根據以上觀點,再結合三段論的方法,可以得出——Java 中的接口也是繼承 Object 類的。因為 Java 的接口也是一種“類”,所以它是繼承 Object 類的。但是事實並非如此。

    先來分析幾段代碼。

    代碼 1

public interface TestObject {
    void test();
    String getString();
}
public static void main(String[] args) {
    Set<String> result = new HashSet<String>();
    for (Method m : TestObject.class.getMethods()) {
        result.add(m.getName());
    }
    System.out.println(result);
}

代碼的輸出結果是:[test, getString],沒有包括 Object 中的任何方法。

代碼 2

public interface I { }
public static void main(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;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM