Fabio Olivetto
Fabio Olivetto

Reputation: 616

lambda function not working

Using this

private static void method (MyClass mc, int num){
    System.out.println(mc.operation(num));
}
private static interface Exec{
    public int operation(int num);
}
static abstract class MyClass implements Exec{}

I am able to call the method using

method(new MyClass(){
    public int operation(int num) {return num*2;}
}, 15);

but when I am using a lambda method(a->a*2, 15); I have 2 errors :

The target type of this expression must be a functional interface

, and

The method ... is not applicable for the arguments (( a) - > {}, int)

Upvotes: 0

Views: 46

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198481

Lambdas can only implement interfaces, they cannot extend abstract classes. If your method had signature

public static void method(Exec mc, int num)

then it would probably work. I strongly suspect both errors are the result of that issue.

Upvotes: 3

Related Questions