user3668541 user3668541 - 1 year ago 70
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
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 Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download