Marker Interface vs Functional Interface in Java

1. Introduction

In Java, an interface is a reference type that can contain only constants, method signatures, default methods, static methods, and nested types. There are special kinds of interfaces in Java known as marker interfaces and functional interfaces. 

A marker interface is an empty interface without any fields or methods, used to signal to the JVM or frameworks that an object of this class will have some special behavior. 

A functional interface, introduced in Java 8, is an interface that contains exactly one abstract method and is used as the basis for lambda expressions in functional programming.

2. Key Points

1. Marker interfaces do not contain any methods or fields and serve as a type to convey metadata about a class.

2. Functional interfaces contain a single abstract method and can have multiple default or static methods.

3. Marker interfaces are used to indicate a signal or a marker, such as Serializable in Java, which tells the JVM that the class can be serialized.

4. Functional interfaces are used to facilitate lambda expressions for functional programming in Java.

3. Differences

Marker Interface Functional Interface
Does not have any methods or fields. Has exactly one abstract method, but can have multiple default and static methods.
Acts as a marker to convey metadata information to compilers or JVM. Enables the use of lambda expressions and functional programming concepts.

4. Example


// Marker Interface example
public interface MyMarkerInterface {
    // Nothing here
}

// Functional Interface example
@FunctionalInterface
public interface MyFunctionalInterface {
    // Only one abstract method
    void execute();

    // Default method, does not count towards the single abstract method criterion
    default void defaultMethod() {
        System.out.println("This is a default method.");
    }
}

// Using the marker interface
class MyClass implements MyMarkerInterface {
    // Some code here
}

// Using the functional interface
class MyFunctionalClass implements MyFunctionalInterface {
    public void execute() {
        System.out.println("Functional interface method implemented.");
    }
}

public class Main {
    public static void main(String[] args) {
        MyFunctionalInterface function = () -> System.out.println("Lambda expression executed.");
        function.execute(); // Executes the lambda expression

        MyFunctionalClass obj = new MyFunctionalClass();
        obj.execute(); // Executes the implemented method in MyFunctionalClass
    }
}

Output:

Lambda expression executed.
Functional interface method implemented.

Explanation:

1. MyMarkerInterface is a marker interface without any methods or fields. Classes implementing this interface might be checked for special handling by some APIs or frameworks.

2. MyFunctionalInterface is a functional interface annotated with @FunctionalInterface and contains one abstract method execute.

3. In the main method, a lambda expression is used to provide an implementation of the execute method, showcasing how a functional interface can be used with lambda expressions.

4. An instance of MyFunctionalClass calls execute, showing how the method is implemented in a class.

5. When to use?

- Use marker interfaces when you need to indicate that a class conforms to a certain behavior without adding any actual functionality.

- Use functional interfaces when you want to capture a single action or behavior, and particularly when you want to use lambda expressions to implement that behavior.

Comments