Cannot override generic interface

核能气质少年 提交于 2019-12-13 05:50:05

问题


I'm trying to create and interface of generic type methods, then implement them in a subclass and so on. But I cannot figure out why my subclass throws an error that says I have not overridden the abstract method from my interface. Code is as follows:

package stdmathops1;
public interface StdMathOps1<T>{
    public <T> T div(T f, T s);
    }
//this is all very simple and not the whole of it, at this point I'm just
//trying to figure out why it won't override the div method.
class Fraction implements StdMathOps1{
    public static void main(String[] args){
    }
    public <T extends StdMathOps1> T div(T f, T s){
        return f;
    }
}

Wherever I look about overriding and using interfaces it seems right, but it still throws

Fraction is not abstract and does not override abstract method div(Object, Object)
in StdMathOps1

Any help would be greatly appreciated


回答1:


You generally can't change bounds on parameters like you did on overridden methods. To the compiler,

public <T> T div(T f, T s);

and

public <T extends StdMathOps1> T div(T f, T s);

are two different methods.

For more information, see Cannot override generic interface



来源:https://stackoverflow.com/questions/23438813/cannot-override-generic-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!