简体   繁体   English

必须将类声明为抽象或实现抽象方法

[英]Class must be declared as abstract or implement abstract method

I have one class and two interfaces 我有一个类和两个接口

public class A implements B, C {
    public static void main(String[] args) {
        A a = new A();
        a.foo();
    }
}

public interface B {
    default void foo(){
        System.out.println("foo in B");
    }
}

public interface C {
    void foo();
}

The thing i'm concerned about is that java doesn't compile this giving an error that I must implement method from C. Hence I have a question. 我关心的是java不编译这个给出一个我必须从C实现方法的错误。因此我有一个问题。 Why doesn't the default body cover that part, I mean the only thing java must be concerned about is that all the methods have their implementations, right? 为什么默认主体不覆盖那部分,我的意思是java必须关注的唯一事情就是所有方法都有它们的实现,对吧? But for the class A, it's obvious that the implementation is given in B. 但对于A类,很明显,实现是在B中给出的。

So why is java giving that error? 那么为什么java会出错呢?

This is because interface B and C are not in same inheritance tree. 这是因为接口B和C不在同一继承树中。 And if they are not then Java compiler cannot be sure that implementing class has implementation of all methods of interface until it checks for each method from each implementing interface. 如果它们不是,那么Java编译器不能确定实现类是否具有所有接口方法的实现,直到它检查每个实现接口的每个方法。

If you will define interface C as public interface C extends B then you will not get the error because in this case Java compiler will be sure that all methods are implemented. 如果您将接口C定义为public interface C extends B那么您将不会收到错误,因为在这种情况下,Java编译器将确保所有方法都已实现。

Read more from JLS §9.4.1. 阅读更多JLS§9.4.1。 Inheritance and Overriding 继承与压倒

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RxAndroid:类必须声明为抽象或实现抽象方法错误 - RxAndroid: Class must either be declared abstract or implement abstract method error 类必须声明为抽象或实现抽象方法toArray - Class must either be declared abstract or implement abstract method toArray 类必须声明为abstract或实现抽象方法:Intellij错误? - Class must either be declared abstract or implement abstract method: Intellij error? 循环的AsyncTask类必须声明为抽象或实现抽象方法 - Looping AsyncTask Class must either be declared abstract or implement abstract method 必须将类声明为abstract或实现抽象方法错误 - Class must either be declared abstract or implement abstract method error 类必须声明为抽象或实现抽象方法错误 - Class must be either declared abstract or implement abstract method error 必须声明为抽象或实现抽象方法 - must either be declared abstract or implement abstract method 类'从AdListener派生的匿名类'必须被声明为abstract或实现抽象方法'onLoggingImpressionMethod(Ad)' - Class 'Anonymous class derived from AdListener' must either be declared abstract or implement abstract method 'onLoggingImpressionMethod(Ad)' 类'从PlaceSelectedListener派生的匿名类'必须声明为抽象或实现抽象方法 - class 'Anonymous class derived from PlaceSelectedListener' must either be declared abstract or implement abstract method TextWatcher 必须要么声明为抽象,要么实现抽象方法 beforeTextChanged - TextWatcher must either be declared abstract or implement abstract method beforeTextChanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM