What is the difference between interface and functional interface?
In Java, both interfaces and functional interfaces define contracts for classes to implement. However, a functional interface is a specialized type of interface with a very specific purpose related to lambda expressions and method references.
Interface
An interface in Java is a blueprint of a class. It can contain method signatures (abstract methods), default methods, static methods, and constant fields. Before Java 8, interfaces could only have abstract methods and constants. With Java 8, default and static methods were introduced, allowing interfaces to provide method implementations. A class that implements an interface must provide an implementation for all its abstract methods.
- Can have any number of abstract methods (zero or more).
- Can include default and static methods (since Java 8).
- Can contain constant fields (implicitly public static final).
- Multiple inheritance of type is supported through interfaces.
- Does not have to adhere to the Single Abstract Method (SAM) rule.
Functional Interface
A functional interface is a special type of interface that contains exactly one abstract method. This single abstract method is known as the 'functional method' or 'SAM (Single Abstract Method) method'. Functional interfaces are crucial for using lambda expressions and method references in Java 8 and later, as they provide the target type for these constructs. They can also have any number of default and static methods, but only one abstract method.
- Must have exactly one abstract method.
- Can contain any number of default and static methods.
- Can be annotated with
@FunctionalInterface(optional, but good practice for compiler checks). - Provides the target type for lambda expressions and method references.
- Follows the Single Abstract Method (SAM) rule.
Core Differences (Summary)
| Feature | Interface | Functional Interface |
|---|---|---|
| Number of Abstract Methods | Zero, one, or many | Exactly one |
| Purpose | Define a contract for behavior, achieve multiple inheritance of type | Provide target type for lambda expressions and method references |
| SAM Rule | Not applicable | Must follow (Single Abstract Method) |
| Annotation | No specific annotation for general interfaces | `@FunctionalInterface` (optional, but recommended) |
| Usage | General contract definition | Primarily used with lambda expressions and method references |
Example of a Regular Interface
interface Shape {
double getArea(); // Abstract method
double getPerimeter(); // Another abstract method
default void printInfo() {
System.out.println("This is a shape.");
}
static String getType() {
return "Geometric Shape";
}
}
Example of a Functional Interface
@FunctionalInterface
interface Calculator {
int operate(int a, int b); // The single abstract method
default void displayOperation() {
System.out.println("Performing an operation.");
}
static int add(int x, int y) {
return x + y;
}
}
// Usage with a lambda expression:
// Calculator addition = (a, b) -> a + b;
// int result = addition.operate(10, 5); // result would be 15