下面的代码中指定 public 或 public abstract 是不必要的吗?

huangapple go评论69阅读模式
英文:

Is it unnecessary to specify public or public abstract in the code below?

问题

interface Airplane {
     String fuelOption = "kerosene";
     
     public abstract String getFuelOption();
}

在包含 getFuelOption() 方法的那一行中,是否有必要指定 publicpublic abstract?谢谢!

英文:
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!

答案1

得分: 2

public access modifier 是不需要的,因为

在接口的方法声明中,每个方法声明都隐式地是 public 的(§6.6)。尽管在接口的方法声明中冗余地指定 public 修饰符是允许的,但出于风格的考虑,不鼓励这样做(第9.4节)。

abstract access modifier 是不需要的,因为

默认方法是在接口中用 default 修饰符声明的方法;它的主体始终由一个代码块表示。

还有...

没有默认修饰符或静态修饰符的接口方法隐式地是抽象的,因此它的主体由分号而不是代码块表示。

鉴于默认方法有主体,而那些没有主体的方法本质上是抽象的,并且接口上的每个方法声明本质上都是 public 的,因此你不需要指定这两个关键字。

请参考以下 JLS(Java 语言规范):

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%20,which%20are%20declared%20in%20classes.

英文:

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.

答案2

得分: 1

不是的,所有的 interface 方法默认都是 publicabstract 的。

来自Java 语言规范第9.4节的相关文本:

接口中的方法可以被声明为 public 或者 private。如果没有提供访问修饰符,该方法会被隐式地视为 public。

没有 private、default 或 static 修饰符的接口方法会隐式地被视为抽象方法。它的主体由分号表示,而不是代码块。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月25日 11:53:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64520195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定