Skip to content

Commit 95ef226

Browse files
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 changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Package1;
2+
3+
interface A {
4+
default void sayHello() {
5+
System.out.println("A says Hello");
6+
}
7+
}
8+
9+
interface B {
10+
default void sayHello() {
11+
System.out.println("B says Hello");
12+
}
13+
}
14+
15+
public class MyClass implements A, B {
16+
@Override
17+
public void sayHello() {
18+
// Combine both interface defaults and add class-specific behavior
19+
System.out.print("Combined: ");
20+
A.super.sayHello();
21+
B.super.sayHello();
22+
System.out.println("MyClass done."); // additional behavior
23+
}
24+
public static void main(String[] args) {
25+
new MyClass().sayHello();
26+
}
27+
}

0 commit comments

Comments
 (0)