user3668541 user3668541 - 7 days ago 5
Java Question

Incompatibe Return Type error when implementing generic interface method

I have a very simple interface:

public interface Evaluateable<T> {
public T evaluate();
}


I am trying to implement the
evaluate()
method in another class:

public class NumberNode() implements Evaluateable {
public double evaluate() { // Error with this method
return 5.0;
}
}


However apparently evaluate() has an 'incompatible return type', despite the interface method being generic. Am I constructing the generic method wrong -- what's happening here?

Thanks in advance.

Answer

You aren't specifying the type to the generic, and you can't use a primitive double (but you can use the Double wrapper type).

public class NumberNode() implements Evaluateable<Double> {
    public Double evaluate() {
        return 5.0;
    }
}