J.II · Java 25 practice question
A developer declares an interface method without private, default, or static, and writes an empty block body for it. What happens when compiling this code?
interface Greeter {
void greet() {}
}
void main() {
IO.println("done");
}
Check answer and explanation
Correct answer: Compilation fails because the method is implicitly abstract and its body must be a semicolon, not a block.
Explanation
Compilation fails because an interface method lacking private, default, or static is implicitly abstract, and an abstract method's body must be represented by a semicolon rather than a block.
Common trap
Java never infers the default modifier from the presence of a method body; default must be written explicitly. An empty block is still a method body and is illegal on an implicitly abstract interface method.
Source: JLS SE 25 §9.4. Method Declarations
Checked against the cited text on 2026-11-25.