Skip to content

Commit d79837e

Browse files
committed
docs(functional-interfaces): add in-depth explanation of functional interfaces and their role in lambdas
What - Added documentation covering the concept of Functional Interfaces in Java. - Detailed explanation includes: 1. Definition → interface with exactly one abstract method. 2. Annotation → use of @FunctionalInterface for compiler enforcement. 3. Purpose → enabling lambda expressions by providing a single abstract target. 4. Built-in examples from `java.util.function` (Runnable, Callable, Comparator, Function, Predicate, Consumer, Supplier). 5. Relation with lambda expressions → lambdas as concise implementations of single abstract methods. 6. Benefits → reduced boilerplate, improved readability, functional programming support. - Included a Quick Recap section for concise review. Why - Clarifies how Functional Interfaces serve as the foundation for Java’s functional programming model introduced in Java 8. - Provides both theoretical understanding and practical examples of their necessity. - Highlights their central role in APIs like Streams and concurrency utilities. Logic 1. Functional Interface = only one abstract method. 2. Can still contain default and static methods (do not count toward abstract method restriction). 3. Lambdas require a functional interface to bind to; compiler uses the interface’s abstract method signature as the contract. 4. Built-in interfaces (Predicate, Function, Supplier, Consumer, etc.) demonstrate common functional patterns. 5. Annotation `@FunctionalInterface` is optional but ensures compile-time safety. 6. Benefits include: - Less boilerplate (compared to anonymous classes). - Contract-based lambda usage. - Cleaner APIs in functional-style programming. Real-life applications - Stream API operations (`map`, `filter`, `forEach`) rely on functional interfaces like Predicate, Function, Consumer. - Concurrency (`Runnable`, `Callable`) simplified with lambdas for background tasks. - Event-driven programming (listeners, callbacks) implemented concisely. - APIs designed with extensibility: adding default/static methods without breaking existing implementations. Notes - Adding more than one abstract method breaks functional interface contract. - Default methods can provide backward compatibility in evolving APIs. - Static methods in interfaces are utility methods, called via the interface name. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8ed7407 commit d79837e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
🔹 Functional Interface in Java:
2+
1. Definition
3+
• A Functional Interface is an interface that contains exactly one abstract method.
4+
• It can have multiple default or static methods, but only one abstract method.
5+
• They are the foundation for Lambda Expressions.
6+
7+
2. Annotation
8+
• Marked with @FunctionalInterface annotation (optional, but recommended).
9+
• Compiler enforces that only one abstract method is present.
10+
11+
3. Why Functional Interface?
12+
• Lambda expressions need a target type (something to map them to).
13+
• Functional Interfaces provide that single abstract method → so lambda knows what to implement.
14+
15+
4. Examples of Built-in Functional Interfaces (from java.util.function package):
16+
• Runnable → void run()
17+
• Callable<T> → T call()
18+
• Comparator<T> → int compare(T a, T b)
19+
• Function<T, R> → R apply(T t)
20+
• Predicate<T> → boolean test(T t)
21+
• Consumer<T> → void accept(T t)
22+
• Supplier<T> → T get()
23+
24+
5. Relation with Lambdas
25+
• A lambda expression is just a shorter way of implementing the single abstract method of a functional interface.
26+
• Example: Instead of writing an anonymous class for Runnable, you just write a lambda.
27+
28+
6. Benefits
29+
• Reduces boilerplate code (no need for long anonymous class definitions).
30+
• Provides a contract for lambda expressions.
31+
• Improves readability, especially in APIs like Streams, where behavior (functions) is passed around.
32+
33+
34+
35+
Quick Recap:
36+
• Functional Interface = exactly one abstract method.
37+
• Marked with @FunctionalInterface.
38+
• Used as the target type for lambdas.
39+
• Java provides many built-in ones (Predicate, Function, Supplier, Consumer).
40+
• Enables functional programming style in Java.

0 commit comments

Comments
 (0)