interface Airplane {
String fuelOption = "kerosene";
public abstract String getFuelOption();
}
Is it necessary to specify public or public abstract in the line with the getFuelOption() method? Thank you!
interface Airplane {
String fuelOption = "kerosene";
public abstract String getFuelOption();
}
Is it necessary to specify public or public abstract in the line with the getFuelOption() method? Thank you!
The public access modifier is not needed because
Every method declaration in the body of an interface is implicitly public (§6.6). It is permitted, but discouraged as a matter of style, to redundantly specify the public modifier for a method declaration in an interface. (Section 9.4)
The abstract access modifier is not needed because
A default method is a method that is declared in an interface with the default modifier; its body is always represented by a block.
And...
An interface method lacking a default modifier or a static modifier is implicitly abstract, so its body is represented by a semicolon, not a block.
Given that default methods have a body, and those that don't are inherently abstract, and every method declaration on an interface is inherently public, you don't need to specify either keyword.
Please refer below JLS :
No, All interface
methods are public
and abstract
by default.
relevant text from the Java language spec, Section 9.4
A method in the body of an interface may be declared public or private .If no access modifier is given, the method is implicitly public.
An interface method lacking a private, default, or static modifier is implicitly abstract. Its body is represented by a semicolon, not a block.