DEV Community

Code Green
Code Green

Posted on • Updated on

Functional Interface in Java

Functional Interface in Java

A functional interface in Java is an interface that contains exactly one abstract method. It can have any number of default methods or static methods.

  • Syntax: Declared using the @FunctionalInterface annotation.
  • Example:
@FunctionalInterface
public interface MyFunctionalInterface {
    void abstractMethod();

    default void defaultMethod() {
        System.out.println("Default method implementation");
    }

    static void staticMethod() {
        System.out.println("Static method implementation");
    }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion: Functional interfaces are the foundation of functional programming in Java, facilitating the use of lambda expressions and method references. They promote concise and readable code by allowing methods to be treated as first-class citizens.

Top comments (0)