Skip to content

Commit a4b5867

Browse files
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 changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
interface A {
2+
// static helper method on interface A
3+
static void util() {
4+
System.out.println("A.util(): interface static method");
5+
}
6+
7+
// default instance method (for comparison)
8+
default void sayHello() {
9+
System.out.println("A says Hello (default)");
10+
}
11+
}
12+
13+
interface B {
14+
static void util() {
15+
System.out.println("B.util(): interface static method");
16+
}
17+
18+
default void sayHello() {
19+
System.out.println("B says Hello (default)");
20+
}
21+
}
22+
23+
public class MyClass implements A, B {
24+
// override required for conflicting default instance methods
25+
@Override
26+
public void sayHello() {
27+
System.out.println("MyClass: resolved default method conflict");
28+
}
29+
30+
// This is a class static method, not an override of A.util()
31+
// (You can define one, but it's separate.)
32+
public static void util() {
33+
System.out.println("MyClass.util(): class static method");
34+
}
35+
36+
public static void main(String[] args) {
37+
// Call interface static methods using the interface name
38+
A.util(); // prints: A.util(): interface static method
39+
B.util(); // prints: B.util(): interface static method
40+
41+
// Call class static method
42+
MyClass.util(); // prints: MyClass.util(): class static method
43+
44+
// Instance default method (overridden)
45+
MyClass obj = new MyClass();
46+
obj.sayHello(); // prints: MyClass: resolved default method conflict
47+
48+
// Trying to call interface static via instance is NOT idiomatic and not allowed:
49+
// obj.util(); // <-- compile-time error: cannot find symbol (util on instance)
50+
// You must use InterfaceName.util()
51+
52+
// If an interface provides a private static helper (Java 9+), it's internal and not visible outside.
53+
}
54+
}

0 commit comments

Comments
 (0)