-1
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!

googaa
  • 1
  • All interface methods are both `public` and `abstract` by default. – user207421 Oct 25 '20 at 04:03
  • It is unnecessary. – Stephen C Oct 25 '20 at 04:06
  • I did... I just wanted to make sure and I saw some people using the key words regardless.. sorry this is my first question.. geez. – googaa Oct 25 '20 at 04:19
  • If you did, then you would have found a definitive answer and not bothered to post. The two people who provided answers answered within just a few minutes, which is atypical for this site. Why? Because the answer was incredibly easy to find via search. You don't need to put on a fig leaf to save face -- all you need to do is to understand that this site values efficiency and move on with that knowledge. Read the help file section on [how to ask](https://stackoverflow.com/help/how-to-ask). – MarsAtomic Oct 25 '20 at 04:32

2 Answers2

2

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 :

https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#:~:text=Every%20method%20declaration%20in%20the,method%20declaration%20in%20an%20interface.&text=Default%20methods%20are%20distinct%20from,which%20are%20declared%20in%20classes.

Mahesh_Loya
  • 2,743
  • 3
  • 16
  • 28
1

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.

mightyWOZ
  • 7,946
  • 3
  • 29
  • 46