functional interface in java

functional interface in java

A functional interface in Java is an interface that contains only one abstract method. Functional interfaces are a key concept in Java’s functional programming paradigm and are used to enable the use of lambda expressions or method references. Despite containing a single abstract method, functional interfaces may also have multiple default or static methods (introduced in Java 8) without violating the functional interface contract.

The @FunctionalInterface annotation is optional but can be used to indicate that an interface is intended to be a functional interface. This annotation helps the compiler catch errors if the interface violates the rules of a functional interface.

Here are the key characteristics of a functional interface:

1. Single Abstract Method (SAM):

  • A functional interface must have exactly one abstract method. This method is often referred to as the Single Abstract Method (SAM).
  • The presence of multiple abstract methods in an interface would violate the functional interface contract.

2. Multiple Default or Static Methods:

  • In addition to the single abstract method, a functional interface can have multiple default or static methods without breaking the functional interface contract.

3. Lambda Expressions and Method References:

  • Functional interfaces are designed to be used with lambda expressions or method references, providing a concise way to represent instances of a single-method interface.

4. @FunctionalInterface Annotation (Optional):

  • The @FunctionalInterface annotation is optional but can be used to explicitly indicate that an interface is intended to be a functional interface.
  • If an interface is annotated with @FunctionalInterface and doesn’t satisfy the conditions (e.g., has more than one abstract method), the compiler generates an error.

Here’s an example of a functional interface:

interface MyFunctionalInterface {
    // Single abstract method
    void myMethod();

    // Default method (allowed)
    default void anotherMethod() {
        System.out.println("Default method");

    // Static method (allowed)
    static void staticMethod() {
        System.out.println("Static method");

In this example, MyFunctionalInterface is a functional interface because it has a single abstract method (myMethod) along with additional default and static methods.

Functional interfaces play a crucial role in supporting functional programming constructs in Java, allowing the use of lambda expressions and providing a more expressive and concise way to work with certain types of interfaces.

Some Built-in Java Functional Interfaces

Certainly! Here are brief definitions and examples for four kinds of functional interfaces in Java:

functional interface in java

1. Function: Represents a function that takes one argument and produces a result.

import java.util.function.Function;

public class FunctionExample {
    public static void main(String[] args) {
        // Function that converts a string to its length
        Function<String, Integer> stringLengthFunction = s -> s.length();

        // Applying the function
        int length = stringLengthFunction.apply("Hello, World!");
        System.out.println("Length of the string: " + length);

2. Consumer: Represents an operation that takes a single input argument and returns no result.

import java.util.function.Consumer;

public class ConsumerExample {
    public static void main(String[] args) {
        // Consumer that prints a message
        Consumer<String> messageConsumer = message -> System.out.println("Message: " + message);

        // Accepting the input
        messageConsumer.accept("Hello, Consumer!");

3. Predicate: Represents a predicate (boolean-valued function) of one argument.

import java.util.function.Predicate;

public class PredicateExample {
    public static void main(String[] args) {
        // Predicate that checks if a number is positive
        Predicate<Integer> isPositive = num -> num > 0;

        // Testing the predicate
        boolean result = isPositive.test(5);
        System.out.println("Is the number positive? " + result);

4. Supplier: Represents a supplier of results, producing a result without taking any input.

import java.util.function.Supplier;

public class SupplierExample {
    public static void main(String[] args) {
        // Supplier that provides a random number
        Supplier<Double> randomSupplier = () -> Math.random();

        // Getting the result from the supplier
        double randomNumber = randomSupplier.get();
        System.out.println("Random number: " + randomNumber);

These examples showcase the use of each functional interface by creating instances through lambda expressions and applying or testing them accordingly. These functional interfaces are part of the java.util.function package introduced in Java 8 and provide a convenient way to work with functional programming constructs.

Leave a Reply

Your email address will not be published. Required fields are marked *