Sub topic 8
Defining Your Own Functional Interfaces
The java.util.function package has many ready-made interfaces. Sometimes none of them fits your task well. When that happens, you can write your own functional interface. This chapter shows you how.
Why Make Your Own?
A built-in interface like BinaryOperator<Integer> works for adding two numbers. But a name like Calculator tells the reader much more about what the code does. A custom interface lets you choose:
- a clear name for the interface
- a clear name for its method, such as
applyorcalculate - exact parameter and return types, with no generics if you don't need them
The @FunctionalInterface Annotation
An annotation is a tag that starts with @ and gives extra information to the compiler. The @FunctionalInterface annotation tells the compiler: "This interface must have exactly one abstract method."
An abstract method is a method with no body. A class or a lambda has to supply the body.
The annotation is optional. Any interface with one abstract method already works with lambdas. The annotation adds a safety check. If someone later adds a second abstract method, the code stops compiling, and the mistake is caught early.
A Worked Example
Here is a Calculator interface with one abstract method, apply. Two lambdas implement it.
@FunctionalInterface
interface Calculator {
int apply(int a, int b);
}
public class CalculatorDemo {
public static void main(String[] args) {
Calculator add = (a, b) -> a + b; // expression body
Calculator multiply = (int a, int b) -> { // explicit types, block body
return a * b;
};
System.out.println(add.apply(2, 3)); // 5
System.out.println(multiply.apply(2, 3)); // 6
}
}Each lambda's parameters match apply's parameters: two int values. Each lambda returns an int, the same as apply.
What Else Can the Interface Hold?
The "one abstract method" rule does not count every method. These are allowed too:
| Member type | Has a body? | Counts toward the limit? |
|---|---|---|
| Abstract method | No | Yes (must be exactly one) |
| `default` method | Yes | No |
| `static` method | Yes | No |
A default method is a method with a body that every implementation gets for free. Here is an example:
@FunctionalInterface
interface Greeter {
String greet(String name);
default String greetLoudly(String name) {
return greet(name).toUpperCase() + "!";
}
}
public class GreeterDemo {
public static void main(String[] args) {
Greeter polite = name -> "Hello, " + name;
System.out.println(polite.greet("Ada")); // Hello, Ada
System.out.println(polite.greetLoudly("Ada")); // HELLO, ADA!
}
}The lambda only provides greet. The greetLoudly method then works with no extra code.
Exercise
Write a functional interface named StringChecker with one method, check, that takes a String and returns a boolean. Then write a lambda that returns true when the string has more than 3 characters.
Show answer
@FunctionalInterface interface StringChecker { boolean check(String s); } and then StringChecker isLong = s -> s.length() > 3; so isLong.check("java") returns true.
Exercise
An interface marked @FunctionalInterface has one abstract method and two default methods. Does it compile? Why?
Show answer
Yes. Default methods have bodies, so they do not count. Only abstract methods count, and there is exactly one.