What are private and private static methods in an interface, and what problem do they solve?
answer
- Java 9 feature — hidden helpers inside an interface
- private instance: callable by default + other private methods
- private static: callable by static + default methods
- Not inherited, not in the public contract
- Purpose: DRY without leaking a public default
basics
~20 sSince Java 9 an interface can have private methods (and private static methods). They are internal helpers that hold shared logic used by the interface's default or static methods, without exposing that logic to implementing classes.
solid answer
~40 sPrivate interface methods, added in Java 9, are helper methods that are part of an interface but not part of its public contract. A private instance method can be called by default methods (and other private instance methods); a private static method can be called by both static and default methods. Their purpose is to remove duplication: before Java 9, if two default methods shared logic, you either repeated it or exposed an awkward public default just to host the shared code — which then leaked into every implementer's API. Private methods let you factor that common code out while keeping it hidden. They cannot be abstract, are not inherited, and cannot be called from outside the interface or from implementing classes. So they're a pure encapsulation tool for the interface's own concrete methods.
code
java · 11 linesinterface Validator {
boolean isValid(String s);
default String require(String s) {
if (!isValid(s)) throw new IllegalArgumentException(fail(s));
return s;
}
private String fail(String s) { // hidden helper, shared logic
return "invalid input: '" + s + "'";
}
}go deeper
Knows interfaces can have private helper methods (Java 9) that hold shared internal code for default/static methods.
Distinguishes private instance vs private static, knows who can call each, and explains they solve duplication without leaking a public default into the contract.
Frames them as an encapsulation/DRY tool that keeps an interface's public surface minimal, and knows the constraints (must have a body, not inherited, not abstract, mutually exclusive with default).
Discusses how private methods let an interface fully own and refactor its concrete behaviour internally, supporting stable public contracts and clean library evolution without companion utility classes.
## Why private interface methods exist Java 8 let interfaces carry concrete code via `default` and `static` methods. But every such method is implicitly **`public`** — it becomes part of the interface's published contract and is visible to every implementer and caller. That created a problem: if two `default` methods needed the **same** internal logic, you had only bad options: 1. **Copy-paste** the logic into both (duplication), or 2. Extract it into another **`default` method** — but that method is now *public*, leaking an implementation detail into the API and into every implementer. Java 9 closed this gap with **private interface methods**. ## The two flavours ```java interface Logger { void emit(String line); default void info(String msg) { emit(decorate("INFO", msg)); } default void error(String msg) { emit(decorate("ERROR", msg)); } private String decorate(String level, String msg) { // private instance helper return "[" + level + "] " + stamp() + " " + msg; } private static String stamp() { // private static helper return java.time.Instant.now().toString(); } } ``` - **`private` (instance) method** — can be called by `default` methods and by other private instance methods. It can use `this` and the interface's abstract/default methods. It is **not** inherited and **not** visible to implementers or outside callers. - **`private static` method** — belongs to the interface type; can be called by both `static` and `default` methods. Use it for helpers that don't need an instance. ## Rules and constraints - A `private` interface method **must have a body** — it cannot be abstract (an abstract private method would be uncallable and contradictory). - It is **never** part of the public contract: implementing classes neither see nor inherit it, and you cannot call it through an instance or the interface name from outside. - A **private instance** method may be invoked only by non-static methods of the same interface (default or other private instance ones); a **private static** method may be invoked by any method (static or default) of the interface. - They cannot be `default` (the two modifiers are mutually exclusive — `default` implies public-and-inherited; `private` is the opposite). ## The point Private interface methods are an **encapsulation / DRY** tool. They let an interface implement its public default and static methods cleanly, sharing internal logic without polluting the contract. They carry no new dispatch semantics — they are simply hidden helpers scoped to the interface.
- Can an implementing class call or override a private interface method?No. Private interface methods are not inherited and are invisible outside the interface, so an implementing class can neither call nor override them.
- Can a private interface method be abstract?No. A private interface method must have a body. An abstract private method would be impossible to implement or call, so the compiler forbids it.
saying these in an interview costs you the question
- Saying private interface methods are part of the contract or visible to implementers.
- Claiming a private static interface method can be called from a private instance method only — actually static helpers are callable from both static and default methods.
- Thinking private interface methods can be abstract.
- Believing they were available in Java 8 (they arrived in Java 9).