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

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup