在一个类中同时实现Super和Sub接口(A类实现SuperInterface,SubInterface) [英] Implementing Super and sub interfaces both in a class(class A implements SuperInterface, SubInterface)

查看:47
本文介绍了在一个类中同时实现Super和Sub接口(A类实现SuperInterface,SubInterface)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface A {

    public void doSomething();
}

interface B extends A {

    public void doSomethingElse();
}

public class AClass implements A, B {

    public void doSomething() {}

    public void doSomethingElse() {}

}

为什么Java允许这样的声明?当通过实现SubInterface(B)可以实现相同的目的时,实现这两个接口有什么用?

Why does Java permit such a declaration? What's the use of implementing both interfaces when same thing can be achieved by implementing the SubInterface (B)?

推荐答案

我认为为什么"问题只能由Java设计人员回答.

I think the "why" question can only be answered by Java designers.

一个原因可能是,它允许将 A扩展到 B ,而不会破坏任何已经实现两者的现有类.

One reason might be that it permits retrofitting extends A to B without breaking any existing classes that already happen to implement both.

使用此构造的另一个原因可能是让 AClass 的最终用户立即清楚该类同时实现了 A B .在 ArrayList中List接口的冗余实现中对此进行了讨论.Java

Another reason for using this construct might be to make it immediately clear to the end user of AClass that the class implements both A and B. This is discussed in Redundant implementation of List interface in ArrayList.java

这篇关于在一个类中同时实现Super和Sub接口(A类实现SuperInterface,SubInterface)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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