Skip to content

Commit 403d274

Browse files
committed
feat(functional-interfaces): demonstrate inheritance of functional interfaces with default and static methods
What - Documented key concept: functional interfaces can extend another functional interface if the total abstract methods = 1. - Explained: - Parent interface → defines 1 abstract method `show(String msg)`, plus a default and a static method. - Child interface → extends Parent, inherits the single abstract method while still being a valid functional interface. - Main class → implements Child via lambda, and demonstrates calling the inherited default and static methods. Why - Reinforces how functional interfaces remain valid when extending another, provided the abstract method count does not exceed one. - Shows that default and static methods are allowed, and do not count towards the single abstract method requirement. - Demonstrates how lambdas can be used with inherited functional interfaces. Logic 1. Define `Parent` with: - Abstract method → `void show(String msg)` - Default method → `defaultMethod()` prints a helper message. - Static method → `staticMethod()` prints a utility message. 2. Define `Child` that extends `Parent`. - Still a functional interface since only 1 abstract method exists. 3. In `Main`: - Implement `Child` with a lambda: `msg -> System.out.println(msg)`. - Use the instance to call `show("Hello from lambda!")`. - Demonstrate `defaultMethod()` (via object) and `staticMethod()` (via interface). Real-life applications - Useful in designing **API hierarchies** where functional interfaces evolve without breaking existing lambda clients. - Allows **default utilities** in functional interfaces for reusability (e.g., logging, validation). - Widely used in Java core libraries (e.g., `java.util.function` package). - Encourages **backward compatibility** when extending older functional interfaces. Notes - Rule: total abstract methods across the interface hierarchy must not exceed 1. - Default + static methods do not affect functional interface status. - If more than 1 abstract method exists, it ceases to be a functional interface. - Annotations like `@FunctionalInterface` help enforce these rules at compile time. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent bd0ff20 commit 403d274

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
🔹 Key Concept:
2+
• A functional interface = only 1 abstract method.
3+
• But it can inherit from another interface if the parent also has only 1 abstract method.
4+
• It can also inherit default methods and static methods without breaking functional interface rules.
5+
6+
🔹 Explanation (for your notes later)
7+
1. Parent interface
8+
• Has 1 abstract method → show(String msg)
9+
• Has 1 default + 1 static method (these don’t break functional interface rule).
10+
11+
2. Child interface
12+
• Extends Parent.
13+
• Since Parent already has exactly 1 abstract method, Child can remain a functional interface.
14+
• You can override the same method, but you cannot add another abstract method.
15+
16+
3. Main class
17+
• Implemented Child with a lambda expression.
18+
• Used defaultMethod() and staticMethod() to show they also work with functional interfaces.
19+
20+
🔹 Quick Recap:
21+
✔ Functional interface can extend another functional interface.
22+
✔ Must ensure total = only 1 abstract method across hierarchy.
23+
✔ Default + static methods are allowed.
24+
✔ Can override parent’s abstract method but cannot add new abstract methods.

0 commit comments

Comments
 (0)