简体   繁体   中英

What is the proper way of overriding 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: " method does not override or implement a method from a supertype"

What is wrong here?

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

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