简体   繁体   中英

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:

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:

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:

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. 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.

This can happen in case you defined your method (thisIsTheMethodThatMatters) in class1 with protected as access modifier. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM