J.II · Java 25 practice question
A developer tries to restrict an interface method to subclasses by marking it protected. What is the result of compiling this Java 25 source file?
interface Service {
protected void execute();
}
void main() {
IO.println("ok");
}
Check answer and explanation
Correct answer: Compilation fails because an interface method may not be declared with protected access.
Explanation
Compilation fails because the Java Language Specification forbids declaring an interface method with protected access.
Common trap
While classes allow protected methods for subclass visibility, interfaces only permit public or private methods. Adding default does not make protected legal on an interface method.
Source: JLS SE 25 §9.4. Method Declarations
Checked against the cited text on 2026-11-25.