Commit 5112423
committed
feat(interfaces): add DefaultMethodExample showcasing abstract, default, and static methods in interfaces
What
- Introduced `Vehicle` interface with:
- `start()` → abstract method requiring implementation.
- `fuel()` → default method providing a base implementation.
- `service()` → static utility method accessible via interface name.
- Added `Car` and `Bike` classes:
- Both override `start()` with class-specific logic.
- `Car` reuses default `fuel()` method.
- `Bike` overrides `fuel()` to provide custom behavior.
- Added `DefaultMethodExample` main driver class to demonstrate usage.
Why
- Default methods (introduced in Java 8) allow adding new functionality to interfaces without breaking existing implementations.
- Static methods in interfaces provide utility-like behavior tied to the interface itself.
- Example demonstrates how to:
1. Implement abstract methods in classes.
2. Use or override default methods flexibly.
3. Call static methods directly from the interface.
Logic
1. **Interface structure**:
- `start()` = abstract, must be implemented in all classes.
- `fuel()` = default method, can be inherited as-is or overridden.
- `service()` = static, called via `Vehicle.service()`.
2. **Class Car**:
- Implements `start()` with ignition logic.
- Inherits default `fuel()` behavior from `Vehicle`.
3. **Class Bike**:
- Implements `start()` with self-start logic.
- Overrides `fuel()` to define petrol-specific behavior.
4. **Main flow**:
- Demonstrates polymorphism (`Vehicle` reference calling concrete methods).
- Shows default method reuse vs. override.
- Calls static method using interface name.
Real-life applications
- API evolution: Adding new methods to interfaces (e.g., Java Collections API methods like `forEach`, `removeIf`).
- Reducing code duplication: Shared logic (e.g., logging, resource checks) can be defined once in a default method.
- Providing common utilities: Static methods inside interfaces (e.g., `Comparator.comparing()`).
Notes
- Default methods resolve backward compatibility problems in evolving APIs.
- If two interfaces provide the same default method, implementing classes must override to resolve ambiguity.
- Static methods in interfaces are **not inherited** and must be called with the interface name.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 1627b97 commit 5112423
File tree
1 file changed
+67
-0
lines changed- Java 8 Crash Course/Functional Interface/Default Methods/src
1 file changed
+67
-0
lines changedLines changed: 67 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 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
0 commit comments