Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way of overriding a generic method?

Tags:

java

generics

public abstract class Abc<T> {
    public abstract void f1(T a);  
}

abstract class Def<T> extends Abc {

    @Override
    public void f1(T a) {
        System.out.print("f");
    }

}

This gives the following error: " method does not override or implement a method from a supertype"

What is wrong here?

like image 569
snappy Avatar asked Feb 21 '23 13:02

snappy


1 Answers

Your class definition needs to indicate that you're extending the parent class generically.

abstract class Def<T> extends Abc<T>

Otherwise, the compiler more or less assumes that you're extending Abc<object>, so the method signature that includes a T parameter doesn't match the one from the parent class (since it's using a different T parameter).

like image 153
StriplingWarrior Avatar answered Mar 07 '23 17:03

StriplingWarrior