在接口中为方法使用抽象关键字与不使用抽象关键字之间的区别
Difference between using an abstract keyword for a method in interface vs not using the abstract keyword
假设我有一个接口 FirstInterface 如下:
public interface FirstInterface {
public void myInterfaceMethod();
}
在这里我声明了一个方法 myInterfaceMethod() ,它将在 class 中定义,它将实现这个接口。
不过我也可以
public interface FirstInterface {
public abstract void myInterfaceMethod();
}
我在方法声明中添加了关键字abstract。
我想知道,如果有的话,在方法声明中添加抽象关键字是否有任何区别?
默认情况下,接口中的所有非默认、非静态方法都是抽象的。添加关键字是无害的,但不会改变任何东西,JLS "as a matter of style."
不鼓励
来自JLS§9.4:
An interface method lacking a default
modifier or a static
modifier is implicitly abstract
... It is permitted, but discouraged as a matter of style, to redundantly specify the abstract
modifier for such a method declaration.
假设我有一个接口 FirstInterface 如下:
public interface FirstInterface {
public void myInterfaceMethod();
}
在这里我声明了一个方法 myInterfaceMethod() ,它将在 class 中定义,它将实现这个接口。
不过我也可以
public interface FirstInterface {
public abstract void myInterfaceMethod();
}
我在方法声明中添加了关键字abstract。 我想知道,如果有的话,在方法声明中添加抽象关键字是否有任何区别?
默认情况下,接口中的所有非默认、非静态方法都是抽象的。添加关键字是无害的,但不会改变任何东西,JLS "as a matter of style."
不鼓励来自JLS§9.4:
An interface method lacking a
default
modifier or astatic
modifier is implicitlyabstract
... It is permitted, but discouraged as a matter of style, to redundantly specify theabstract
modifier for such a method declaration.