Sitemap
3 min readApr 5, 2025

--

In Java, functional interfaces and marker interfaces serve different purposes, and understanding the difference is important for understanding how Java works, especially in the context of lambda expressions, streams, and object-oriented design.

1. Functional Interface:

A functional interface is an interface that has exactly one abstract method. It can have multiple default or static methods, but there must be exactly one abstract method. These interfaces are used primarily to represent lambda expressions and method references in Java, especially when working with functional programming paradigms.

Key Points:

  • It must have exactly one abstract method.
  • It can have multiple default or static methods.
  • It can be annotated with the @FunctionalInterface annotation, although this is optional. The annotation is used to ensure the interface has only one abstract method and provides a compile-time check.

Example of a Functional Interface:

@FunctionalInterface
interface MyFunctionalInterface {
void myMethod(); // This is the single abstract method
// You can have default or static methods
default void defaultMethod() {
System.out.println("This is a default method");
}
static void staticMethod() {
System.out.println("This is a static method");
}
}
public class FunctionalInterfaceExample {
public static void main(String[] args) {
// Lambda expression implementing the functional…

--

--

No responses yet