What are the implicit (default) modifiers on interface methods and fields, and why does this matter?
answer
- Methods → public abstract
- Fields → public static final (constants)
- Can't narrow to protected/private (abstract)
- Final field needs an inline initializer
- Constant-interface = anti-pattern
basics
~10 sIn an interface, abstract methods are automatically public abstract, and fields are automatically public static final (constants). You don't have to write those modifiers; the compiler adds them.
solid answer
~50 sInterface members carry implicit modifiers so you rarely write them. Every abstract method is implicitly `public abstract` — public because the interface is a contract for callers, abstract because it has no body. Every field is implicitly `public static final`, i.e. a compile-time constant: shared (static), unchangeable (final), and visible to all (public); because it's final you must initialize it inline. This matters in practice: you cannot weaken visibility — declaring an interface method `protected` or `private` (for an abstract method) is a compile error, since you can't make a contract method less accessible than its implicit `public`. `default` and `static` methods are also implicitly public but have bodies. Knowing these defaults prevents confusion like 'why can't I assign to this interface field?' (it's final) or 'why is my package-private interface method illegal?' (abstract methods must be public).
go deeper
Recites that interface methods are public abstract and fields are public static final.
Explains you cannot narrow visibility and that fields are immutable constants requiring an initializer.
Connects implicit public to Liskov/override rules and warns against the constant-interface anti-pattern.
Discusses API surface implications of implicit publicity and chooses enum/utility class over constant interfaces in framework design.
## Modifiers, briefly A **modifier** is a keyword that adjusts a member's access or behavior: `public`/`protected`/`private` control *visibility* (who can see it); `static` means it belongs to the type itself rather than to an instance; `final` means it cannot be reassigned; `abstract` means it has no body and must be implemented elsewhere. Inside an interface, Java applies several modifiers **implicitly** — the compiler treats them as present even if you omit them. Writing them is legal but redundant (and tools often flag the redundancy). ## Abstract methods → implicitly `public abstract` ```java interface Service { void run(); // identical to: public abstract void run(); } ``` - **`public`**: an interface is a contract meant to be *called*, so its methods are visible to everyone. You literally **cannot** narrow this — `private void run();` or `protected void run();` as an *abstract* method is a compile error. You can't make a contract method less accessible than the contract. - **`abstract`**: the method has no body; an implementing class supplies it. ## Fields → implicitly `public static final` (constants) ```java interface Limits { int MAX = 100; // identical to: public static final int MAX = 100; } ``` - **`public`**: visible everywhere as `Limits.MAX`. - **`static`**: one shared value belonging to the interface, not to any instance (interfaces have no instances anyway). - **`final`**: it is a constant — you must assign it at declaration and can never reassign it. `Limits.MAX = 5;` is a compile error. Because of `final`, an interface field **must** have an initializer. Because of `static`, it is not per-object state — interfaces hold no instance state. ## Methods with bodies - **`default` methods** (Java 8+) are implicitly `public` and have a body; implementors inherit them. - **`static` methods** (Java 8+) are implicitly `public`, called on the interface (`Comparator.naturalOrder()`). - **`private` methods** (Java 9+) are the one place an interface method is *not* public — they are internal helpers for default/static methods and must have a body. ## Why it matters 1. **You can't weaken visibility** — overriding a `public` interface method in a class must also be `public` (an implementor cannot reduce access). This is part of the Liskov substitution guarantee: the contract stays at least as accessible. 2. **Interface fields are constants, not config** — a common mistake is to put mutable 'settings' in an interface; you can't, because they're `final`. 3. **Constant interfaces are an anti-pattern** — using an interface purely to hold `public static final` constants leaks them into the type's API and is discouraged (use an enum or a final class with a private constructor instead). Knowing the implicit modifiers explains *why* the constants leak: they become part of every implementor's namespace. ## Summary table | Member | Implicit modifiers | |---|---| | abstract method | `public abstract` | | field | `public static final` | | default method | `public` (has body) | | static method | `public static` (has body) | | private method (Java 9+) | `private` (has body) |
- Why is declaring an interface's abstract method `protected` a compile error?Abstract interface methods are implicitly `public`; you cannot reduce a contract method's accessibility. An implementing class also can't override it with weaker access.
- What is the 'constant interface' anti-pattern?Defining an interface solely to hold `public static final` constants, then implementing it to import them. It leaks the constants into the implementor's public API and ties unrelated types together. Prefer an enum or a final utility class with a private constructor, accessed via static import.
saying these in an interview costs you the question
- Thinking you must write `public` on interface methods (redundant)
- Believing interface fields can be mutable or reassigned
- Trying to make an abstract interface method `private` or `protected`
- Assuming interface fields are per-instance