Commit a4b5867
committed
feat(interfaces): add example demonstrating static methods, default methods, and conflict resolution
What
- Created `interface A` and `interface B`:
- Both declare a static utility method `util()` with unique implementations.
- Both provide a default instance method `sayHello()`.
- Implemented `MyClass` that implements both A and B:
- Overrides `sayHello()` to resolve the diamond problem (conflicting defaults).
- Defines its own class-level static method `util()` (distinct from interface static methods).
- In `main`:
- Invokes static methods from `A.util()` and `B.util()`.
- Invokes `MyClass.util()` to show class-level static.
- Creates an instance of `MyClass` and calls the overridden `sayHello()`.
Why
- Demonstrates key Java 8+ interface enhancements:
- **Static methods in interfaces**: callable via `InterfaceName.method()`, never inherited.
- **Default methods**: provide reusable behavior but require resolution if multiple interfaces define the same method.
- **Conflict resolution rule**: class must override when two interfaces provide the same default method.
- Clarifies the separation:
- Interface static methods ≠ Class static methods (no overriding).
- Default methods can be inherited but must be disambiguated in case of collisions.
Logic
1. Static methods in interfaces:
- Invoked using `InterfaceName.util()`.
- Not accessible through implementing class instances.
2. Default methods:
- Inherited if no conflict.
- If conflict exists (A.sayHello vs B.sayHello), implementing class **must** override.
3. Class static method in `MyClass`:
- Independent of interface static methods.
- Demonstrates namespace separation.
4. Execution flow in `main`:
- Prints messages from A.util, B.util, MyClass.util.
- Instance call prints the resolved overridden method.
Real-life applications
- **Library evolution**: default methods allow adding new behaviors to interfaces without breaking existing implementations.
- **Utility methods**: static interface methods are a clean way to package helper logic (like `Comparator.comparing()`).
- **Conflict handling**: common in mixin-style design where multiple interfaces provide reusable methods.
Notes
- Interface static methods are hidden, not overridden by implementing classes.
- Default methods can call `InterfaceName.super.method()` if partial delegation is desired.
- Since Java 9, interfaces can also have private static and **private instance methods for internal reuse.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent a7b0e71 commit a4b5867
File tree
1 file changed
+54
-0
lines changed- Java 8 Crash Course/Functional Interface/Static Methods/src
1 file changed
+54
-0
lines changedLines changed: 54 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 | + | |
0 commit comments