Is there a way to use lambdas if the target class has more than one interface methods? Or do you just have to use an anonymous inner class in that case?
-
You mean, in a same class, you have two methods potentially "implementing the interface"? Then use a method reference – fge Apr 14 '14 at 00:25
-
1Have a look [here](http://stackoverflow.com/questions/22663112/java-8-streams-while-does-this-compile-part-2-or-what-is-a-method-reference) also; what you can actually use as a "lambda" is... Broad. – fge Apr 14 '14 at 00:27
-
4You can use lambda expressions as a replacement for anonymous inner class implementations that use *functional interfaces*. Functional interfaces can only have one explicitly declared abstract method. – helderdarocha Apr 14 '14 at 00:35
2 Answers
No, there isn't. If I understood your question correctly you'd want to use lambdas for interfaces with more than one abstract method. In that case the answer is negative:
A functional interface is any interface that contains only one abstract method. (A functional interface may contain one or more default methods or static methods.) Because a functional interface contains only one abstract method, you can omit the name of that method when you implement it. To do this, instead of using an anonymous class expression, you use a lambda expression [...]
Read it up there: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

- 5,248
- 4
- 31
- 47
-
3In addition to the quote... There can be **more than one** abstract method, if these methods match those implemented by Object, leaving only one abstract method unimplemented. This case is a bit surprising to discover. – uvsmtid Nov 23 '16 at 09:58
-
@uvsmtid Thanks for adding that. It clarified exactly what I was trying to answer. – Drew Apr 08 '20 at 16:41
It is not possible to directly create a multi-method object with a lambda.
But, you can use a work-around to solve the problem in a pretty neat way:
Use a util method that takes a number of single-method objects as arguments, and returns a multi-method object, into which the single-method objects have been packed.
Example:
interface MultiMethodInterface {
void method1();
String method2(String arg);
}
public static void example() {
// One lambda for each method, they are packed into a
// MultiMethodInterface object by the multiMethodInterface method
MultiMethodInterface i1 = createMultiMethodObject(
() -> System.out.println("method1"),
arg -> "method2: " + arg);
// Sometimes only one of the methods is used, a specialized wrapper
// can be used if that is common
MultiMethodInterface i2 =
createMethod1Wrapper(() -> System.out.println("method1"));
}
public static MultiMethodInterface createMultiMethodObject(
Runnable methodAction1,
Function<String, String> methodAction2)
{
return new MultiMethodInterface() {
@Override
public void method1() {
methodAction1.run();
}
@Override
public String method2(String arg) {
return methodAction2.apply(arg);
}
};
}
public static MultiMethodInterface createMethod1Wrapper(Runnable methodAction1) {
return createMultiMethodObject(methodAction1, arg -> "");
}
The resulting code is at least a bit shorter and prettier than to create a anonymous class. At least if the method implementations are short, and/or only one of the methods needs to be implemented.
This technique is used for example in the SWT toolkit, to create listener objects.

- 11,553
- 8
- 64
- 88
-
-
@RaviParekh: In that case, instead of `Runnable` you can use one of the consumer or function interfaces, for example [`java.util.function.BiConsumer`](https://docs.oracle.com/javase/8/docs/api/java/util/function/BiConsumer.html). If you have more than two arguments you have to create your own special purpose interface to substitute `Runnable`. – Lii Oct 27 '18 at 13:12
-
@RaviParekh I updated the answer to demonstrate how to handle methods with arguments. – Lii Oct 28 '18 at 12:15