Java 子類實(shí)現(xiàn)父類已經(jīng)實(shí)現(xiàn)的接口

結(jié)論

假設(shè)有

  1. class A implements Interface
  2. class B extends A
  3. class C extends A implements Interface.

那么:

  • B和C都可以向上轉(zhuǎn)型為Interface
  • B和C在不覆蓋接口定義的方法時(shí)都會(huì)用父類的實(shí)現(xiàn)
  • 在反射方法getInterfaces()中, A和C類都會(huì)有Interface, 但是B不會(huì)有Interface

驗(yàn)證

Viewable接口:

package interfaces;

/**
 * Created by xiaofu on 17-11-5.
 */
public interface Viewable {
    void view();
}

BasePhoto類:

package interfaces;

/**
 * Created by xiaofu on 17-11-5.
 */
public class BasePhoto implements Viewable {

    @Override
    public void view() {
        System.out.println("viewing base photo");
    }
}

Selfie類:

package interfaces;

/**
 * Created by xiaofu on 17-11-5.
 */
public class Selfie extends BasePhoto {

    // 可以直接覆蓋父類實(shí)現(xiàn)的接口方法, 而不用再在class聲明中再寫(xiě)implement Viewable了
    @Override
    public void view() {
//        super.view();
        System.out.println("viewing my selfie");
    }
}

Landscape類:

package interfaces;

/**
 * Created by xiaofu on 17-11-5.
 * 父類已經(jīng)實(shí)現(xiàn)了Viewable接口
 */
public class LandscapePhoto extends BasePhoto implements Viewable {

    @Override
    public void view() {
        System.out.println("viewing landscape photo");
    }
}

Main類:

package interfaces;

/**
 * Created by xiaofu on 17-11-5.
 */
public class Main {

    public static void testImplementation(){
        Viewable basePhoto = new BasePhoto();
        Viewable selfie = new Selfie();
        Viewable landscape = new LandscapePhoto();
        basePhoto.view();
        selfie.view();
        landscape.view();
        // 輸出
        // viewing base photo
        // viewing my selfie
        // viewing landscape photo
    }

    public static void testRelection(){
        BasePhoto basePhoto = new BasePhoto();
        BasePhoto selfie = new Selfie();
        BasePhoto landscape = new LandscapePhoto();
        System.out.println("basePhoto has Viewable interface: " + hasInterface(basePhoto));
        System.out.println("selfie has Viewable interface: " + hasInterface(selfie));
        System.out.println("landscape has Viewable interface: " + hasInterface(landscape));
        // 輸出
        // basePhoto has Viewable interface: true
        // selfie has Viewable interface: false
        // landscape has Viewable interface: true
    }

    private static boolean hasInterface(BasePhoto photo){
        Class<?>[] interfaces = photo.getClass().getInterfaces();
        for(Class<?> c: interfaces)
            if (c.equals(Viewable.class))
                return true;
        return false;
    }

    public static void main(String[] args) {
        testImplementation();
        testRelection();
    }

}

如果實(shí)現(xiàn)的接口有類型參數(shù)

該類情況, 子類要么不聲明implements直接Override父類中該接口的方法, 要么聲明的時(shí)候只能用和父類一樣的類型參數(shù).
原因如下(原鏈接):

A class may not at the same time be a subtype of two interface types which are different parameterizations of the same generic interface (§9.1.2), or a subtype of a parameterization of a generic interface and a raw type naming that same generic interface, or a compile-time error occurs.
This requirement was introduced in order to support translation by type erasure (§4.6).

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容