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 |
---|---|
An interface with no methods or fields. Its presence serves as an indication or a marker for the Java compiler or JVM about the special treatment of the class implementing it. | An interface that contains exactly one abstract method. It may contain default and static methods but only one abstract method. |
Used as a tagging mechanism to convey metadata information to the compiler or runtime about the objects of the classes implementing these interfaces. Examples include Serializable, and Cloneable. | This is used to enable lambda expressions in Java. Lambda expressions can implement functional interfaces on the fly. Examples include Runnable, Callable, and Comparator. |
It cannot be used to assign a lambda expression. | Enables the assignment of lambda expressions to variables of the interface's type. |
Primarily used before annotations were introduced in Java 5 to add metadata to a class. | Introduced in Java 8 as part of the language's functional programming capabilities, enhancing support for lambda expressions and method references. |
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. Conclusion
Marker interfaces and functional interfaces serve different purposes in Java. While marker interfaces are used as a form of tagging to influence runtime behavior or compiler treatment, functional interfaces are integral to expressing lambdas and method references, thereby facilitating functional programming patterns in Java.
Comments
Post a Comment
Leave Comment