Domanda

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

È stato utile?

Soluzione

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top