What is the correct way to override a generic method?

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: "the method does not override or does not implement the method from the supertype"

What is wrong here?

+4
source share
1 answer

Your class definition should indicate that you are extending the parent class as a whole.

 abstract class Def<T> extends Abc<T> 

Otherwise, the compiler more or less assumes that you extend Abc<object> , so the method signature that includes the T parameter does not match that of the parent class (since it uses a different T parameter).

+8
source

Source: https://habr.com/ru/post/1395326/


All Articles