Search code examples
javaabstract-class

But I DID override that abstract method


I must be an idiot, but I'm just not getting what my error is here.

final class EmptyBench extends MicroBench {
    long doIterations(long numIterations) throws InterruptedException {
        return numIterations;
    }
}

This extends the class MicroBench containing this declaration:

abstract long doIterations(long numIterations) throws InterruptedException;

I get this error:

EmptyBench.java:6: error: EmptyBench is not abstract and does not override abstract method doIterations(long) in MicroBench

There are no generics in sight, no change in return types, no change in the throws clause. Can someone tell me what I'm missing here?


Solution

  • Using these classes, I can reproduce the problem:

    package a;
    
    public abstract class A {
        abstract void method();
    }
    

    and

    package b;
    
    import a.A;
    
    public final class B extends A {
        void method() {}
    }
    

    However, my IDE (Eclipse) gave me a different error message, so YMMV:

    This class must implement the inherited abstract method A.method(), but cannot override it since it is not visible from B. Either make the type abstract or make the inherited method visible

    I don't have anything to add to this error message, except that, if you don't control the MicroBench class, you're out of luck.

    As a side note, if B was abstract instead of final, then you could create a class a.C extends b.B that in turn would be able to implement a.A.method()