Functional Interfaces

 
/**
 * FUNCTIONAL INTERFACES
 * ---------------------
 * A functional interface is an interface with one abstract method.
 * 
 * Example:
 *  - Runnable
 *  - Comparator<T>
 *  - Predicate<T>
 *  - BinaryOperator<T>
 * 
 * @FunctionalInterface
 * public interface Predicate<T> {
 *      boolean test(T var1); // abstract
 *      ...
 * 
 * Functional interfaces are commonly used with lambdas, streams, 
 * and method references. 
 */

package com.minte9.lambdas.functional_interfaces;

import java.util.function.Function;
import java.util.function.Predicate;

public class FunctionalInterfaces {
    public static void main(String[] args) {

        // -----------------------------------------
        // Predicate using a lambda expression
        // -----------------------------------------
        Predicate<Integer> isEven = x -> x % 2 == 0;
        System.out.println(isEven.test(36));  // true

        // -------------------------
        // Function example
        // ----------------------------
        Function<Long, Long> increment = x -> x + 1;
        System.out.println(increment.apply(20L));  // 21
    }
}

Custom Functional Interface

 
/**
 * CUSTOM FUNCTIONAL INTERFACE
 * ---------------------------
 * You can define your own functional interface when:
 *   - built-in ones (Predicate, Function, etc) don't fit
 *   - you want clearer domain-specific meaning
 * 
 * A functional interface may containt:
 *   - exactly ONE abstract method
 *   - zero or more default methods
 *   - zero or more static methods
 * 
 * Only the abstract method counts toward the "functional" rule.
 * 
 * A method reference works when the referenced method
 * has the SAME signature as the functional interface method.
 */

package com.minte9.lambdas.functional_interfaces;

public class CustomFunctionalInterface {
    public static void main(String[] args) {

        MyFuncInterface exclaim = s -> s + "!";
        MyFuncInterface question = s -> s + "?";

        System.out.println(exclaim.run("Hello World"));  // Hello World!
        System.out.println(question.run("Java Lambdas"));  // Java Lambdas?

        System.out.println(exclaim.describe());  // MyFuncInterface: transforms a String

    }
}

// Custom functional interface
@FunctionalInterface
abstract interface MyFuncInterface {

    // Single abstract method (SAM)
    String run(String s);

    // Default method (does NOT count as abstract)
    default String describe() {
        return "MyFuncInterface: transforms a String";
    }


}






Questions and answers:
Clink on Option to Answer




1. What is a functional interface?

  • a) An interface with multiple abstract methods
  • b) An interface with exactly one abstract method

2. Which of the following is a functional interface?

  • a) Predicate
  • b) ArrayList

3. What does the @FunctionalInterface annotation enforce?

  • a) That the interface cannot be extended
  • b) That the interface has exactly one abstract method

4. Can a functional interface contain default methods?

  • a) Yes, as long as there is only one abstract method
  • b) No, it can only contain one method total

5. Why are functional interfaces commonly used with lambdas?

  • a) Because lambdas create objects automatically
  • b) Because lambdas implement the single abstract method

6. When should you create a custom functional interface?

  • a) When built-in functional interfaces do not fit your use case
  • b) When you want to replace all existing interfaces

7. What determines whether a method reference can be used?

  • a) The method name must match
  • b) The method signature must match the abstract method

8. In a functional interface, which methods count toward the “one method” rule?

  • a) Only abstract methods
  • b) Abstract, default, and static methods


References: