Types of Interfaces
- Normal – any interface which has more than one method, the one we create typically
- SAM – Single Abstract method // Java 1.8 – name changed to Functional Interface
- Marker – interface with no methods
Functional Interface – an interface with one abstract method
Lambda expressions
Works only with functional interfaces – interfaces with only one method.
Below is an example of an interface with only one method which calculates the square of a given integer. We have used an anonymous inner class to provide the implementation.
interface CalcSquare {
int square(int i);
}
public class Demo {
public static void main(String[] args) {
CalcSquare obj = new CalcSquare() {
@Override
public int square(int i) {
return i * i;
}
};
int result = obj.square(5);
System.out.println(result); // 25
}
}
Converting the above implementation to lambda expressions :
All the code marked in blue is something that the compiler can deduce based on interface name and the method declaration.
We can omit that code and change it to something like this :
CalcSquare obj = (int i) -> {
return i * i;
};
Further we can shorten the code :
1. If you have only one statement as part of the function body, you can remove braces {} also.
2. If the function has only a return statement, we can remove the return keyword also.
3. We can in fact, no need to mention datatype of the parameter, it can be deduced from the interface .
CalcSquare obj = (i) -> i * i;
Lambda expression Visually
-> arrow symbol differentiates between parameters and function body.
If your method has only one parameter, you can in fact remove () around the parameter as well.
CalcSquare obj = i -> i * i;
Can we implement lambda expressions if we don’t have a functional interface? No, lambda expressions only work with functional interfaces, meaning interfaces with only one method.
Syntax :
Scenarios
If I have an interface which has one default method and one abstract method, can I use lambda expressions with such an interface ?
Yes, because it is still considered as a single abstract method interface.
interface CalcSquare {
// default method
default void show() {
System.out.println("show in interface");
}
// abstract method
int square(int i);
}
public class Demo {
public static void main(String[] args) {
CalcSquare obj = (i) -> i * i;
int result = obj.square(6);
System.out.println(result); // 25
}
}
Lambda Expressions – Additional Notes
History
One of the major feature that got released in Java 8, which is none other than Lambda expression.
Lambda Expressions will allow you to write less code and achieve more.

Along with the OOPs concept there is also called functional programming has risen its importance because it is very well suited for concurrent and event driven programming like reactive programming.
–> the main objective of Lambda expressions that got introduced in Java 8 is to bring the functional programming features and style into Java without affecting object oriented language.
What is a Lambda expression?
A Lambda expression is a block of code that you can pass around so it can be executed later, once or multiple times.
It is an anonymous function. That means the function does not have any name, return type and access modifiers.
One of the great features of the lambda expression is, you can pass your entire business logic as a behavior to another method or variable so that it can be executed any number of times.
What I mean here is, just like how you pass method parameters, like if you want to pass a data to a method, you pass them inside input parameters. Similarly, you can also pass the business logic that you want to be executed using lambda expression. So that’s the main purpose of it.
You can also pass your functionality or code as a method argument using lambda expressions.