0

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

2
  • You are adding a condition, changing your method so its not actually overriding. The method in your interface specifies any class type <T> while your attempt of overriding specifies a class type that must extend or fall under STDMathOps1
    – Vince
    Commented May 3, 2014 at 0:25
  • Possible duplicate of Cannot override generic interface
    – awksp
    Commented May 3, 2014 at 0:27

1 Answer 1

2

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

2
  • They don't have the same erasure.
    – newacct
    Commented May 3, 2014 at 21:26
  • You're right. My mistake, forgot that erasures on bounded types erase to the bounded type.
    – awksp
    Commented May 3, 2014 at 21:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.