What is Functional Interface in Java

๐Ÿ’ก Concept: Functional Interface in Java

A functional interface is an interface with exactly one abstract method, designed to be implemented by a lambda expression or method reference.

๐Ÿ“˜ Quick Intro

Functional interfaces are a foundation of Java 8's functional programming features, enabling concise lambda syntax.

๐Ÿง  Analogy

Think of a functional interface as a single job description, perfect for handing off to a helper without ambiguity.

๐Ÿ”ง Technical Explanation

  • Has exactly one abstract method (SAM - Single Abstract Method).
  • Can have default and static methods.
  • Annotated with @FunctionalInterface (optional but recommended).
  • Used primarily with lambda expressions and method references.
  • Examples: Runnable, Callable, Comparator.

๐ŸŽฏ Use Cases

  • โœ… Simplify anonymous class implementations.
  • โœ… Enable functional programming constructs.
  • โœ… Used extensively in Streams and other Java 8 APIs.

๐Ÿ’ป Example: Functional Interface in Java


@FunctionalInterface
interface MyFunctionalInterface {
    void execute();
}

public class FunctionalInterfaceExample {
    public static void main(String[] args) {
        MyFunctionalInterface fi = () -> System.out.println("Executing functional interface");
        fi.execute();
    }
}

โ“ Interview Q&A

Q1: What is a functional interface?
A: An interface with exactly one abstract method.

Q2: Is @FunctionalInterface annotation mandatory?
A: No, but recommended.

Q3: Can functional interfaces have default methods?
A: Yes.

Q4: Name some examples of functional interfaces.
A: Runnable, Callable, Comparator.

Q5: How do functional interfaces relate to lambda expressions?
A: Lambda expressions implement functional interfaces.

Q6: Can functional interfaces have multiple abstract methods?
A: No.

Q7: What is SAM?
A: Single Abstract Method.

Q8: Can functional interfaces extend other interfaces?
A: Yes, but must still have only one abstract method.

Q9: Are functional interfaces thread-safe?
A: Depends on implementation.

Q10: Can functional interfaces be used without lambdas?
A: Yes, with anonymous classes.

๐Ÿ“ MCQs

Q1. What is a functional interface?

  • Interface with multiple methods
  • Interface with one abstract method
  • Class
  • Enum

Q2. Is @FunctionalInterface annotation mandatory?

  • Yes
  • No
  • Sometimes
  • Always

Q3. Can functional interfaces have default methods?

  • No
  • Yes
  • Sometimes
  • Never

Q4. Name some functional interfaces

  • Runnable, Callable
  • Runnable, Callable, Comparator
  • Thread, Process
  • None

Q5. How do lambda expressions relate to functional interfaces?

  • Ignore them
  • Implement functional interfaces
  • Create classes
  • None

Q6. Can functional interfaces have multiple abstract methods?

  • Yes
  • No
  • Sometimes
  • Always

Q7. What is SAM?

  • Single Abstract Method
  • Static Abstract Method
  • Single Action Method
  • None

Q8. Can functional interfaces extend other interfaces?

  • No
  • Yes
  • Sometimes
  • Never

Q9. Are functional interfaces thread-safe?

  • Yes
  • No
  • Depends
  • Always

Q10. Can functional interfaces be used without lambdas?

  • No
  • Yes
  • Sometimes
  • Never

๐Ÿ’ก Bonus Insight

Functional interfaces enable Java's powerful functional programming features, facilitating cleaner and more flexible code.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

๐Ÿ’ฌ Feedback
๐Ÿš€ Start Learning
Share:

Tags: