Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource

Table of Contents

Functional Interface in Java – A complete Guide

Since Java is an Object-Oriented Language, everything within it revolves around Java Classes and Objects. This means no function operates independently in Java. The introduction of the Functional Interface in Java 8 enabled functional programming, like in Python. According to Statista, Java is among the most popular programming languages with 30.55% of developers using it.   

In Java, all the functions are part of a class, and to call upon any function, it has to be done with the class or object of the class. The Functional Interface in Java makes it easier to write codes because of the modular approach of the Functional Interface.   

It also makes the whole code more readable and concise and reduces the risk of introducing bugs into the code. A Functional interface in Java is an interface that has only one abstract method. Learn about what it is, how it works, syntax, examples, and much more! 

Table of Contents

1) What is Functional Interface in Java? 

     a) What is Lambda expression? 

     b) What is abstract method? 

2) Ways to use Functional Interface 

     a) Using Functional Interface through Lambda Expression 

     b) Using Functional Interface by implementing the Interface 

3) Types of Functional Interface 

4) Conclusion 

What is Functional Interface in Java? 

Before understanding Functional Interface, it is essential to know how things worked before its introduction in Java 8. Earlier, standard interfaces were used, which only had abstract methods and constants. Any class that used the Interface had to implement all its methods or be declared abstract.  

Functional Interface can contain any number of default and static methods but only one abstract method. It is also called a Single Abstract Method interface (SAM). Lambda expressions are used to represent the Functional Interfaces. 

Want to learn more advanced concepts in Java and become a better programmer? Sign up for the Java Programming course now. 

What is Lambda Expression? 

Lambda Expression was added in Java 8, which made Functional Interface possible. Earlier, you would have to create a class for every piece of functionality. Lambda Expressions allows you to pass or return from any method.  

Ideally, Lambda Expressions must be specific in expressing the functionality it has. 

Example 
 

public class LambdaExample
{
public static void main(String[] args)
{
Runnable r = () -> System.out.println("Hello, world!");
r.run();
}
}


This example creates a new 'Runnable' object using a Lambda expression. The 'Runnable' interface has only one abstract method called run(), which takes no arguments and returns no value.  

Instead of defining a separate class that implements the 'Runnable' interface, we can use a Lambda expression to directly create a new instance of the 'Runnable' interface.   

This Lambda Expression is a short function that takes no arguments and prints "Hello, world!" to the console. 

() -> System.out.println("Hello, world!")  

Finally, it calls the run() method on the 'Runnable' object to execute the Lambda expression and print the message "Hello, world!" to the console. 

What is abstract method? 

An abstract method is a type of method that is only declared but not defined in a class. It has no implementation details and behaves like a placeholder for any subclass declaring the abstract method. The abstract method ensures that any subclass of the declaring class implementing the method has a common behaviour across all subclasses. 

Example 

Let's say you have an abstract class called 'Animal'. 'eat()', 'sleep()', and 'move()' can be a set of common methods and properties that all animals share in the class 'Animal'. However, the 'Animal' class doesn't provide any implementation details for these methods; instead, it simply declares them abstract. 
 

public abstract class Animal
{
public abstract void eat();
public abstract void sleep();
public abstract void move();
}


By defining these methods as abstract, the 'Animal' class declares that any subclass of 'Animal' must implement these methods. For example, you might have a subclass called 'Dog' that extends 'Animal'. This class would need to implement the 'eat()', 'sleep()', and 'move()' methods to be a valid subclass of 'Animal'
 

public class Dog extends Animal
{
public void eat()
{
System.out.println("The dog is eating");
}
public void sleep()
{
System.out.println("The dog is sleeping");
}
public void move()
{
System.out.println("The dog is running"); }
}


By using the required methods in the 'Dog' subclass, you can be sure that any instance of the 'Dog' class will have the required behaviour of an animal. 
 

java Training
 

Ways to use Functional Interface 

Now that you know Java Functional Interface, it's time to learn how to use them. There are two ways to use Functional Interface. One method uses the Lambda expression, and another implements the Interface (overriding the abstract method). 

Using Lambda Expression to use Functional Interface 

Lambda expressions are very similar to the normal methods used in Java, but they make the overall code concise, smaller and more readable. Here are some things you should remember when using Lambda expression to implement Functional Interface.  

You always have to write the @FunctionalInterface above the interface declaration itself. It can either be passed as a parameter for a method or even be returned as a value since it acts as a function. Only one abstract method can be used in the Interface, or the compiler will mark it as an Unexpected @FunctionalInterface annotation.   

You do not have to mention the abstract keyword before the method because every method in the Functional Interface is marked as abstract only.   

Let's take an example to understand how to use Lambda expression to implement Functional Interface. Let's say you want to have a Functional Interface that calculates the area of a rectangle. You can define the Interface like this. 
 

@FunctionalInterface 

public interface RectangleAreaCalculator
{
public double calculateArea(double length, double width);
}


After defining the Functional Interface, you can implement its single abstract method using Lamba expression. Let's say you want to implement a 'RectangleAreaCalculator' interface; you can do it like this: 

RectangleAreaCalculator calculator = (length, width) -> length * width;

This example uses the lambda expression '(length, width) -> length * width' to take two parameters as length and width and returns the product of it. The lambda expression is then assigned to 'RectangleAreaCalculator', the Functional Interface implemented earlier.

When you are done implementing the Functional Interface with a Lambda expression, you can use it like any other object in your code. You can use the 'calculator' variable to calculate the area of a rectangle like this.

double area = calculator.calculateArea(5.0, 10.0); // area is 50.0

In this case, the 'calculateArea' is the method, and the 'calculator' is the object with arguments '5.0' and '10.0'. This method uses a Lambda expression to calculate the area of the rectangle to the assigned 'area' variable. 

Using Functional Interface by implementing the Interface (overriding the abstract method)

Using the Functional Interface implements the Interface in the class and overrides the abstract method. To call the method, you have to create the object of the class and then use the syntax obj.AbstractMethod().

First, you must define the Functional Interface with a single abstract method like before. Let's take the same example of calculating the area of a rectangle. The first step of implementing the Functional Interface with the abstract method will remain the same. 
 

@FunctionalInterface 
public interface RectangleAreaCalculator
{
public double calculateArea(double length, double width);
}


Next, you must create a class that implements the Functional Interface and then overrides the abstract method. This is where the code differs from the method used earlier. 
 

public class CalculateRectangleArea implements RectangleArea
{
@Override
public double calculate(double length, double width)
{
return length * width;
}
}


Here, the 'CalculateRectangleArea' class implements the method 'calculate' of the 'RectangleArea' interface that takes the length and width to return the area value.  

Now that you have implemented the Functional Interface by overriding the abstract method, you can use it like any object in the code like this. 

RectangleArea areaCalculator = new CalculateRectangleArea(); 

double length = 5.0; 

double width = 3.0; 

double area = areaCalculator.calculate(length, width); 

System.out.println("The area of the rectangle is: " + area); 

Here, an instance of the 'CalculateRectangleArea' class is created and assigned to a variable 'RectangleArea', which is the Functional Interface. The method calculates the rectangle area and returns it as a double value stored in the 'area' variable. 

Learn to build applications from scratch with our Introduction To Java EE Training Course. 

Types of Functional Interface 


Functional Interfaces

These are the four common types of Functional Interface used in Java 

a) Predicate 

b) Consumer 

c) Supplier 

d) Function 

Predicate 

This type of Functional Interface represents a function that takes in an argument and returns a Boolean value based on a condition. For example, it can check whether a given number is even or odd. 

Here is an example of a Predicate Functional Interface 
 

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class PredicateExample
{
public static void main(String[] args)
{
List numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
Predicate isEven = n -> n % 2 == 0;
List oddNumbers = numbers.stream()
.filter(isEven.negate())
.toList();
System.out.println("Odd numbers: " + oddNumbers);
}
}


Here, the 'Predicate' Functional Interface named 'isEven' checks whether the given integer is even by using the modulo operator to check whether the remainder is zero after dividing it by two. Further, it uses the 'Stream' class' 'filter()' method to create a list of odd numbers by negating the' isEven' predicate using the 'negate()' method. 

Consumer 

This is a type of Functional Interface that takes in an argument but does not return any value. For example, you can print out a list of names. 

Here is an example of a consumer Functional Interface. 
 

import java.util.function.Consumer;
public class ExampleConsumer
{
public static void main(String[] args)
{
Consumer printLength = (String str) -> System.out.println("Length of string: " + str.length());
printLength.accept("Hello, world!"); // Output: Length of string: 13
}
}


Here, the 'Consumer' Functional Interface called 'printlength' that takes in a 'string' and prints it. The Lambda expression '(String str) -> System.out.println("Length of string: " + str.length())’ defines the behavior of the Consumer interface. Next, the 'accept method' of the 'Consumer' interface takes in the string "Hello, world!" as an argument, which results in the output "Length of string: 13". 

Supplier 

Supplier Functional Interface is the type which doesn't take in any arguments but returns a value. For example, you can use the Supplier to generate a random number or alphabet.

Here's an example of a Supplier Functional Interface 
 

import java.util.function.Supplier;
public class ExampleSupplier
{
public static void main(String[] args)
{
Supplier randomInt = () -> (int) (Math.random() * 100);
System.out.println("Random number: " + randomInt.get());
}
}


Here, the 'Supplier< Integer' Functional Interface called the 'randomInt' returns a random integer between 0 and 100. The lambda expression used here defines the behaviour of the 'Supplier' interface. Using 'get' method, a random integer is generated as output. 

Function 

The function is a type of Functional Interface that returns a value after taking in a single argument. For example, you can Function to convert a string into an integer. 

Here’s an example of function Functional Interface:  
 

import java.util.function.Function;
public class ExampleFunction
{
public static void main(String[] args)
{
Function square = (Integer x) -> x * x;
System.out.println("Square of 5: " + square.apply(5)); // Output: Square of 5: 25
}
}


Here, the 'FunctionFunctional Interface calls the 'square' that takes in an 'Integer' and returns its square value. The lambda expression used here defines the behaviour of the 'Function' interface. The 'apply' method of the interface takes in '5' as an argument and returns the square value. 

Some built-in Functional Interfaces 

Post Java 8, many interfaces were converted into Functional Interfaces, and these are annotated with @FunctionalInterface. These interfaces are given below. 

a) Runnable: Contains only the run() method 

b) Comparable: Contains only the compareTo() method 

c) ActionListener: Contains only the actionPerformed() method 

d) Callable: Contains only the call() method 

Conclusion 

To sum it up, Java Functional Interface enables programmers to write code more concisely and modularly. The best part about the Functional Interface is that it significantly reduces bloat in the code, making debugging easier. There are a lot of predefined Functional Interface in Java to implement them using method references and lambda expressions, and you can use your own custom interfaces. 

Want to learn more advanced concepts in Java and become a better programmer? Sign up for our comprehensive Java Programming Course today!

Frequently Asked Questions

What is encapsulation in Java? faq-arrow

Encapsulation in Java is a mechanism that binds the data (variables) and methods (functions) acting on the data into a single unit known as a class. It restricts access to some of the object's components, promoting data hiding and protection, ensuring that the internal implementation details are hidden from the outside world.

What are the rules for Functional Interface? faq-arrow

Functional Interfaces in Java have three rules:

a) It must have only one abstract method.

b) It can have multiple default or static methods.

c) It must be annotated with the @FunctionalInterface annotation to ensure that it only has one abstract method, making it clear for functional programming and compatibility with lambda expressions.
 

What is Knowledge Pass, and how does it work? faq-arrow

The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.

What are related Courses and blogs provided by The Knowledge Academy? faq-arrow

The Knowledge Academy offers various Java Training including Java Programming, JavaScript for Beginners and Java Swing Development Training. These courses cater to different skill levels, providing comprehensive insights into How to Become a Java Developer.

Our Programming and DevOps blogs cover a range of topics related to Java, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Java Programming skills, The Knowledge Academy's diverse courses and informative blogs have you covered.
 

What are the other resources provided by The Knowledge Academy? faq-arrow

The Knowledge Academy takes global learning to new heights, offering over 30,000 online courses across 490+ locations in 220 countries. This expansive reach ensures accessibility and convenience for learners worldwide.

Alongside our diverse Online Course Catalogue, encompassing 17 major categories, we go the extra mile by providing a plethora of free educational Online Resources like News updates, Blogs, videos, webinars, and interview questions. Tailoring learning experiences further, professionals can maximise value with customisable Course Bundles of TKA.
 

Upcoming Programming & DevOps Resources Batches & Dates

Date

building Java Programming

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

OUR BIGGEST SPRING SALE!

Special Discounts

red-starWHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.