Commit 95ef226
committed
feat(interfaces): resolve default method conflict with multiple inheritance
What
- Added `MyClass` implementing both interfaces `A` and `B`, each of which defines a default method `sayHello()`.
- Overrode `sayHello()` in `MyClass` to:
1. Print `"Combined: "` prefix.
2. Explicitly call `A.super.sayHello()` and `B.super.sayHello()` to invoke both parent interface implementations.
3. Add class-specific behavior with `"MyClass done."`.
Why
- Demonstrates how Java handles the **diamond problem** (conflicts when two interfaces provide the same default method).
- Reinforces that classes must explicitly override the conflicting method, ensuring deterministic behavior.
- Shows how to **reuse default method implementations** from multiple interfaces instead of discarding them.
Logic
1. **Interface A**: Provides `sayHello()` printing `"A says Hello"`.
2. **Interface B**: Provides `sayHello()` printing `"B says Hello"`.
3. **MyClass**:
- Must override `sayHello()` because of conflict between `A` and `B`.
- Uses qualified calls:
- `A.super.sayHello()` → invokes A’s default method.
- `B.super.sayHello()` → invokes B’s default method.
- Adds `"MyClass done."` to mark class-level extension.
4. **Main Method**:
- Creates an instance of `MyClass`.
- Calls overridden `sayHello()`.
- Output sequence demonstrates combined behaviors.
Real-life applications
- **API design**: When multiple interfaces provide default methods, implementing classes must decide how to resolve ambiguity.
- **Code reuse**: Default methods let interfaces carry reusable functionality without forcing all implementers to re-implement.
- **Frameworks**: Common in mixin-style designs (e.g., logging, auditing, monitoring interfaces) where multiple behaviors need to be composed.
- **Backward compatibility**: Default methods allow library maintainers to add new methods to interfaces without breaking old implementations.
Notes
- Without explicit override in `MyClass`, compilation fails due to ambiguity.
- Developers can choose to:
- Call one parent’s implementation (`A.super.sayHello()`).
- Call both (as shown).
- Provide entirely new logic discarding parents.
- This pattern is essential for resolving multiple inheritance conflicts in Java’s interface system.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent a4936a9 commit 95ef226
File tree
1 file changed
+27
-0
lines changed- Java 8 Crash Course/Functional Interface/Default Methods/src/Package1
1 file changed
+27
-0
lines changedLines changed: 27 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 | + | |
0 commit comments