Green Cup

FUNCTIONAL INTERFACE IN JAVA

MASTER FUNCTIONAL PROGRAMMING FUNCTIONAL INTERFACE IN JAVA

WHAT IS FUNCTIONAL INTERFACE ?

A Functional Interface is an interface with exactly one abstract method. It’s used for lambda expressions and method references in Java 8 and later.

WHY USE FUNCTIONAL INTERFACES ?

✅ Enables functional programming in Java ✅ Reduces boilerplate code ✅ Supports clean, readable lambda expressions

KEY RULES OF FUNCTIONAL  INTERFACES

– Only one abstract method – Can have default and static methods – Annotated with @FunctionalInterface (optional but recommended)

COMMON BUILT-IN FUNCTIONAL INTERFACES

– Runnable – Callable – Comparator<T> – Function<T, R> – Predicate<T> – Consumer<T>

EXAMPLE OF A FUNCTIONAL INTERFACES

@FunctionalInterface interface MyInterface {    void sayHello(); } MyInterface mi = () -> System.out.println("Hello!"); mi.sayHello();

FUNCTIONAL INTERFACE WITH PARAMETER

@FunctionalInterface interface Adder {    int add(int a, int b); } //lambda Adder sum = (a, b) -> a + b; System.out.println(sum.add(5, 3));