0

While exercising lambdas in Java 8, I came across the below functional interface.

@FunctionalInterface
interface MyFunctional {
  void invokeSame(MyFunctional myFunc);
}

Could you tell me how can I provide lambda expression for this Functional interface?

2 Answers 2

2

The same way as any other one argument and void return type lambda:

MyFunctional f = myFunc -> {};
Sign up to request clarification or add additional context in comments.

Comments

0

Concept: Type checking in java 8 lambdas is done based on target (the function to which it is being passed as an argument).

Explanation: Generally a function is defined by 3 attributes:

  1. Function Name.
  2. Type and order of arguments passed.
  3. Return type of function.

But while passing an lambda Java only checks for last two. So for above Functional Interface we need lambda with [first] argument type MyFunctional and return type void.

void doSomething(MyFunctional x) {
...
...
  x.invokeSame(...An object/lambda of type MyFunctional...)
}

To call this function
doSomething(varName -> { ...someCode... })

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.