Interface Implementation with Lambda Expressions in Java


Create a Java program to demonstrate interface implementation with lambda expressions

  • The addition operation, the lambda expression (a, b) -> a + b performs addition of the two input integers a and b.
  • The subtraction operation, the lambda expression (a, b) -> a - b performs subtraction of the second integer b from the first integer a.

Source Code

interface Operation
{
	int perform(int a, int b);
}
 
public class Main
{
	public static void main(String[] args)
	{
		Operation addition = (a, b) -> a + b;
		Operation subtraction = (a, b) -> a - b;
 
		System.out.println("Addition : " + addition.perform(4, 7));
		System.out.println("Subtraction : " + subtraction.perform(10, 7));
	}
}

Output

Addition : 11
Subtraction : 3

Example Programs