Commit 8ed7407
committed
feat(functional-interfaces): add example of inheritance with default and static methods
What
- Introduced `Parent` interface marked with `@FunctionalInterface` containing:
- One abstract method → `show(String msg)`.
- One default method → `defaultMethod()` for demonstration.
- One static method → `staticMethod()` for utility purposes.
- Created `Child` interface extending `Parent`.
- Overrode `show(String msg)` but did not add new abstract methods (still valid as a functional interface).
- Added `InheritanceFunctionalInterfaceExample` class:
- Implemented `Child` via a lambda expression.
- Showcased usage of abstract, default, and static methods in functional interfaces.
Why
- Demonstrates that a functional interface can extend another functional interface **as long as the total number of abstract methods remains one**.
- Reinforces that **default** and **static methods** do not break the single abstract method rule.
- Highlights best practices for designing reusable and backward-compatible APIs with functional interfaces.
Logic
1. Define `Parent`:
- `show(String msg)` → abstract method (functional interface core).
- `defaultMethod()` → prints "Parent default method".
- `staticMethod()` → prints "Parent static method".
2. Define `Child`:
- Extends `Parent`.
- Overrides `show(String msg)` but adds no new abstract methods, maintaining functional interface integrity.
3. Main class `InheritanceFunctionalInterfaceExample`:
- Implements `Child` using a lambda: `msg -> System.out.println("Lambda says: " + msg)`.
- Calls:
- `child.show(...)` → executes lambda.
- `child.defaultMethod()` → invokes inherited default implementation.
- `Parent.staticMethod()` → invokes interface static method.
Real-life applications
- Enables extending existing functional interfaces without breaking lambda-based code.
- Useful for evolving APIs with **default helpers** (logging, validation) while preserving functional style.
- Foundational pattern in Java standard library (`java.util.function` interfaces like `Function`, `Consumer`).
- Helps create clean, expressive APIs that balance **functional programming** with **object-oriented design**.
Notes
- `@FunctionalInterface` enforces compile-time safety ensuring only one abstract method exists.
- If multiple abstract methods are added, the interface ceases to be functional, breaking lambda support.
- Default methods → can be overridden in implementing classes.
- Static methods → must be called via interface name, not via object reference.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 403d274 commit 8ed7407
File tree
1 file changed
+42
-0
lines changed- Java 8 Crash Course/Functional Interface/Inheritance In Functional Interface/src
1 file changed
+42
-0
lines changedLines changed: 42 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 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
0 commit comments