elect elect - 1 month ago 6
Java Question

Overriding abstract method with an extended interface as parameter

I have the following abstract class:

public abstract class A {

protected abstract void initBuffer(GL3 gl);
}


and an extending class of A:

public class Aimplementation extends A {
@Override // error
protected void initBuffer(GL4 gl);
}


Now,
GL4
interface extends
GL3


public interface GL4 extends GL4ES3, GL3{


So I thought, ok, I can override it, but it turned it out I can't


method does not override or implement a method from a supertype


I also tried with generic:

protected abstract <T extends GL3> void initBuffer(T gl);


No way

Why can't
Aimplementation.initBuffer()
override
A.initBuffer()
?

Loc Loc
Answer

Why can't Aimplementation.initBuffer() override A.initBuffer()?

CAN but the way you did is overloading, Not overriding

You can do like this

public abstract class A<T extends GL3> {

    protected abstract void initBuffer(T gl);
}

public class Aimplementation extends A<GL4> {

    @Override
    protected void initBuffer(GL4 gl) {

    }
}