Commit 403d274
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- Java 8 Crash Course/Functional Interface/Inheritance In Functional Interface/src
1 file changed
+24
-0
lines changedLines changed: 24 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
0 commit comments