Java is a object oriented programming language where objects and classes are the main entities. If we create a function, it should exists within a class. Function has no meaning outside the scope of the class.
In java 8, the authors of java tried to add elements of functional programming so they introduced lambda expression.
What is functional programming?
In functional programming, functions are treated as first-class citizens, meaning that they can be assigned to variables, passed as arguments to other functions, and returned from functions. This makes it possible to create functions that are independent of any particular class or object.
What is Lambda Expression?
It is an anonymous function, i.e a function with no name and identifier. We can pass it to other methods as parameters.
Using Object Oriented Programming approach
@FunctionalInterface
public interface Pets {
void performAction();
}
// Class representing a Cat
public class Cat implements Pets {
// Implementing the performAction method from the Action interface
@Override
public void performAction() {
System.out.println("The cat meows!");
}
}
// Class representing a Dog
public class Dog implements Pets {
// Implementing the performAction method from the Action interface
@Override
public void performAction() {
System.out.println("The dog barks!");
}
}
public class Main {
public static void action(Pets pets){
pets.performAction();
}
public static void main(String[] args) {
// Create instances of Dog and Cat
Pets dog = new Dog();
Pets cat = new Cat();
// Call the action method for each instance
action(dog);// Output: The dog barks!
action(cat); // Output: The cat meows!
}
}
Using anonymous class approach
@FunctionalInterface
public interface Pets {
void performAction();
}
public class Main {
public static void action(Pets pets){
pets.performAction();
}
public static void main(String[] args) {
// Call the performAction method for each instance
action(new Pets(){
@Override
public void performAction(){
System.out.println("The dog barks!");
}
}); // Output: The dog barks!
action(new Pets(){
@Override
public void performAction(){
System.out.println("The cat meows!");
}
}); // Output: The cat meows!
}
}
Using Lambda Expression
@FunctionalInterface
public interface Pets {
void performAction();
}
public class Main {
public static void action(Pets pets){
pets.performAction();
}
public static void main(String[] args) {
// Passing lambda expression to action method
action(() -> System.out.println("The dog barks!")); // Output: The dog barks!
action( () -> System.out.println("The cat meows!")); // Output: The cat meows!
}
}
Note:
Lambda expressions only applicable to functional interfaces, which is an interface that contains only one abstract method. However, they can include any quantity of default and static methods. In Functional interfaces, there is no need to use the abstract keyword as it is optional to use the abstract keyword because, by default, the method defined inside the interface is abstract only. We can also call Lambda expressions as the instance of functional interface.
Functional interfaces are used and executed by representing the interface with an annotation called @FunctionalInterface.