Skip to content

Commit 6af9ff2

Browse files
committed
feat(interfaces): add explanation and examples for static methods in interfaces
What - Documented the concept of **static methods in interfaces** (introduced in Java 8). - Explained their behavior, declaration rules, and contrast with default methods. - Added clear guidance on: - How to declare (`static` keyword with a body). - How to invoke (`InterfaceName.method()`). - Why they cannot be overridden by implementing classes. - How Java 9+ supports `private static` for internal helpers. - Provided concise FAQ and example scenarios (e.g., A.util(), MyClass.util()). Why - Java interfaces before 8 could only declare abstract methods → no shared utility logic. - With Java 8, static methods allow grouping related helpers *inside the API contract itself* (e.g., `Collections`, `Map.of()`). - Improves discoverability: utility methods tied to an interface appear directly in IDE autocompletion. - Clarifies key distinction: - **Default methods** → instance-level, can be inherited/overridden. - **Static methods** → interface-level, cannot be overridden or inherited. Logic 1. **Declaration** - Must have a body in the interface. - Example: ```java interface A { static void util() { System.out.println("helper"); } } ``` 2. **Call site** - Invoked using the interface name: ```java A.util(); ``` - Cannot be called via an instance (`obj.util()` → compile error). 3. **Non-overriding rule** - If an implementing class defines a static method with the same signature, it is a separate method, not an override. - Call resolution depends on qualifier (`InterfaceName.method()` vs `ClassName.method()`). 4. **Java 9+ enhancement** - `private static` methods allowed for factoring out code used by multiple default/static methods. - Keeps interfaces cleaner by hiding internal helpers. 5. **Inheritance model** - Static methods do not participate in polymorphism (no super/virtual dispatch). - They belong strictly to the interface declaration. Real-life applications - **API helpers**: `Comparator.comparing()`, `Map.of()`, `List.of()` are static factory methods that simplify usage. - **Encapsulation**: interface groups both behavior contracts (abstract/default) and related utilities (static). - **Consistency**: avoids scattering utility methods into unrelated classes (like `Utils` classes). Notes - Interface static methods are `public` by default, but can also be `private` (Java 9+). - Cannot be abstract. - Good practice: use static methods for factory methods, validators, or other utilities closely tied to the interface. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent a4b5867 commit 6af9ff2

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
What are static methods in interfaces and why they exist:
2+
3+
• Since Java 8 interfaces can contain both default and static methods.
4+
• A static method in an interface is a utility/helper method that belongs to the interface itself —
5+
not to instances or implementing classes.
6+
• Call it only with the interface name: MyInterface.helper().
7+
• Cannot be overridden by implementing classes. If a class defines a method with the same signature,
8+
it is a different method (not overriding the interface static).
9+
• Static interface methods are useful for grouping related helpers with the API (like Collections, Map.of, etc.).
10+
• Visibility: public by default if you declare it static—you can also use other modifiers
11+
(e.g., private static inside interfaces since Java 9 for helper methods).
12+
• They do not participate in inheritance polymorphism: neither super dispatch nor virtual dispatch.
13+
14+
15+
2) Rules / Behavior (concise):
16+
• Declaration: static methods must have a body in the interface.
17+
• Call site: InterfaceName.staticMethod(...).
18+
• Implementing class cannot override or change the interface static method behavior for callers that use
19+
InterfaceName.method().
20+
• If both interface and class have the same signature static method,
21+
the call depends on reference (className.method vs interfaceName.method) — no polymorphism.
22+
• private static methods (Java 9+) can be used inside the interface to factor out code used by other default/static
23+
methods.
24+
25+
1. Kya hai:
26+
- Interface ke andar `static` method = helper / utility method jo interface ka hi hota hai.
27+
- Isko call karna hai: InterfaceName.method().
28+
29+
2. Key Rules:
30+
- Implementing class static method = NOT overriding; alag method samjho.
31+
- Instance se call nahi kar sakte (obj.method() nahi chalega).
32+
- Default methods = instance-level (override kar sakte ho), static = interface-level (cannot override).
33+
34+
3. Use-cases:
35+
- API-level utilities related to interface.
36+
- Group helper functions with the interface for discoverability.
37+
38+
4. Java 9+:
39+
- `private static` allowed inside interface for internal helpers.
40+
41+
5. Quick Examples:
42+
- `A.util()` → calls interface A's static util.
43+
- `Package2.MyClass.util()` → class-level static method (different).
44+
- `obj.util()` → compile error.
45+
46+
47+
✔ interface static = belongs to interface only.
48+
✔ call: InterfaceName.method().
49+
✔ cannot be overridden; the implementing class can define its own static method but they are independent.
50+
✔ default ≠ static: default is instance-level and can be overridden.
51+
52+
53+
Short FAQ:
54+
• Q: Can I call interface static method from implementing class without interface name?
55+
A: You should call it with the interface name. You can call it from the class, but still use InterfaceName.method().
56+
57+
• Q: Are interface static methods inherited?
58+
A: No polymorphic inheritance. They belong to the interface declaration; call them by their interface.
59+
60+
• Q: Can static methods be abstract?
61+
A: No — static methods must have a body in interfaces.

0 commit comments

Comments
 (0)