Java 子类是否继承接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20534726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:35:13  来源:igfitidea点击:

Do subclasses inherit interfaces?

javapolymorphism

提问by munchschair

Quick question, I'm learning about interfaces and inheritance.

快速提问,我正在学习接口和继承。

This is not actual code, just an example. Let's say I have the abstract class Animal. There's some inheritance with groups like horses, and canines. There's also an interface "Pets". It's gonna be used on different subclasses of Animal. The subclass of canine "Dog" implements the interface "Pets". Therefore all subclasses of "Dog" also implement the interface "Pet" without having to individually implement "Pets" on each subclass of "Dog", right?

这不是实际代码,只是一个示例。假设我有抽象类动物。有一些像马和犬类这样的群体的遗产。还有一个界面“宠物”。它将用于 Animal 的不同子类。犬类“Dog”的子类实现了接口“Pets”。因此,“Dog”的所有子类也实现了“Pet”接口,而不必在“Dog”的每个子类上单独实现“Pets”,对吗?

采纳答案by csvan

If you have:

如果你有:

abstract class StaffMember implements MyInterface

where

在哪里

interface MyInterface {
    void myMethod();
} 

then all of the classes extending StaffMember will inherit the type MyInterface, and you will be able to refer to them by this base type in other parts of the code where a MyInterface instance is expected as an operand/argument, for example:

那么所有扩展 StaffMember 的类都将继承 MyInterface 类型,并且您将能够在代码的其他部分通过此基类型引用它们,其中 MyInterface 实例应作为操作数/参数,例如:

void otherMethod(MyInterface param) { //... }

The actual implementation of the interface MyInterface can take place either in the abstract class, or in any of the classes extending the abstract class. The important thing is simply, in this case, that myMethod() is specified somewhere in the inheritance hierarchy, so that the JVM can find a definition of it to invoke.

接口 MyInterface 的实际实现可以发生在抽象类中,也可以发生在任何扩展抽象类的类中。重要的是,在这种情况下,在继承层次结构中的某处指定了 myMethod(),以便 JVM 可以找到它的定义以进行调用。

回答by Jeroen Vannevel

No.

不。

An interface defines how a class should look like (as a bare minimum). Whether you implement this in a base class or in the lowest subclass doesn't matter.

接口定义了类的外观(作为最低要求)。无论您是在基类中还是在最低的子类中实现它,都没有关系。

The interface has to be entirely implemented throughout the hierarchy of sub classes and base class and has to be defined at the level where the interface implementation signature is located (implements Interface).

接口必须在整个子类和基类的层次结构中完全实现,并且必须在接口实现签名所在的级别定义 ( implements Interface)。

The sub classes themselves have no knowledge about the interface, but they have the implicit connection trough their base class.

子类本身对接口一无所知,但它们通过基类具有隐式连接。

Because I'm a kind person:

因为我是个善良的人:

public class Test {
    public static void main(String[] args) {
        BaseClass obj = new SubClass();
        obj.test();
        obj.test2();

        SomeInterface obj2 = new SubClass();
        obj2.test();
        obj2.test2();
    }
}

interface SomeInterface {
    public void test();

    public void test2();
}

abstract class BaseClass implements SomeInterface {
    @Override
    public abstract void test();

    @Override
    public void test2() {
        try {
            System.out.println(Arrays.toString(this.getClass().getMethod("test2", null).getDeclaringClass().getInterfaces()));
        } catch (NoSuchMethodException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   
}

class SubClass extends BaseClass {
    @Override
    public void test() {
        try {
            System.out.println(Arrays.toString(this.getClass().getMethod("test", null).getDeclaringClass().getInterfaces()));
        } catch (NoSuchMethodException | SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}   

}

}

Output:

输出:

[]
[interface SomeInterface]
[]
[interface SomeInterface]

As you can see it shows that test()(which is implemented in SubClass) does not return any interfaces implemented, while test2()(which is implemented in BaseClass) does show that an interface is implemented by that class. Abstract classes can allow to implement the methods from an interface they implement to be implemented in sub classes by marking the definition as abstract.

如您所见,它表明test()(在 中实现SubClass)不返回任何已实现的接口,而test2()(在 中实现BaseClass)确实表明接口是由该类实现的。通过将定义标记为 ,抽象类可以允许从它们实现的接口实现方法,以便在子类中实现abstract

And as you can see in the mainmethod, both defining the object as BaseClassor SomeInterfaceworks and makes no difference.

正如您在main方法中所看到的,将对象定义为BaseClass或 都SomeInterface有效并且没有区别。