user6008337
user6008337

Reputation: 231

unable to use an interface with generic extending Number

public static void main (String[] args) {
    test((a,b)->a+b, 2, 3);
}

private static void test (Op a, int num1, int num2){
    System.out.println(a.op(num1, num2));
}

private static interface Op <T extends Number>{
    public T op(T num1, T num2);
}

in the second line i have bad operand types for binary operator +. What is it i am supposed to do to make this work ?

Upvotes: 2

Views: 29

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

By adding the type to Op in test like

private static void test(Op<Integer> a, int num1, int num2) {
    System.out.println(a.op(num1, num2));
}

Then your code will output 5 (as I think you expected).

Upvotes: 1

Related Questions