简体   繁体   English

Java错误:方法未从超类型重写或实现方法

[英]java error: method does not override or implement a method from a supertype

I have 3 classes the following configuration: Class1 has a few methods, some of them abstract: 我有3个类,它们的配置如下:Class1有一些方法,其中一些是抽象的:

package package1;

public abstract class class1{
    protected abstract void methodX();

    public boolean methodY(){
        //method implementation
    }

    public String thisIsTheMethodThatMatters(){
        //method implementation
    }
}

Next, class2 from the same package extends class1, overrides its abstract methods and adds a few other: 接下来,同一包中的class2扩展了class1,覆盖了其抽象方法并添加了其他一些方法:

package package1;

public class class2 extends class1{
    @Override
    protected final void methodX(){
        //method implementation
    }

    public boolean methodZ(){
        //method implementation
    }
}

Finally, class3 from a different package extends class2 and overrides a method from class1: 最后,来自另一个包的class3扩展了class2并覆盖了class1中的方法:

package package2;

import package1.class2;

public class class3 extends class2{
    @Override
    public String thisIsTheMethodThatMatters(){
        //method implementation
    }
}

I'm using Eclipse, and it doesn't detect any error when coding. 我正在使用Eclipse,并且在编码时不会检测到任何错误。 I'm building the project using Maven Build, but building fails saying that thisIsTheMethodThatMatters() does not override or implement a method from a supertype I'm making sure that package1 is built before package2, hence I don't think the problem is there. 我正在使用Maven Build构建项目,但是构建失败时说thisIsTheMethodThatMatters() 不会覆盖或实现超类型的方法 ,因此我要确保package1在package2之前构建,因此我认为问题不存在。

This can happen in case you defined your method (thisIsTheMethodThatMatters) in class1 with protected as access modifier. 如果您在class1中定义了方法(thisIsTheMethodThatMatters),并且将其作为访问修饰符,则可能会发生这种情况。 In other words if the method's signature was 换句话说,如果方法的签名是

protected String thisIsTheMethodThatMatters()

then you will get the error because the method will NOT be inherited by class3 based on the above scenario 那么您将收到错误,因为基于上述情况该方法将不会被class3继承

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

相关问题 Java“方法没有覆盖或实现来自超类型的方法”错误 - Java "method does not override or implement a method from a supertype" error 方法不会从父类型@override覆盖或实现方法编译错误 - method does not override or implement a method from a supertype @override compile error 错误:(124,9)错误:方法未覆盖或从超类型实现方法 - Error:(124, 9) error: method does not override or implement a method from a supertype 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 错误:方法未覆盖或从超类型实现方法 - error: method does not override or implement a method from a supertype 错误:方法未覆盖或从超类型OnCreateOptionsMenu实现方法 - error: method does not override or implement a method from a supertype OnCreateOptionsMenu 方法不会从超类型错误中覆盖或实现方法 - method does not override or implement a method from a supertype error React Native - 错误:方法不会覆盖或实现超类型的方法 - React Native - error: method does not override or implement a method from a supertype 错误:方法onEnable不会覆盖或实现超类型的方法 - Error: method onEnable does not override or implement a method from a supertype
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM