如您所知,在 java 接口(interface)中,所有方法都必须定义为抽象方法。但是,当我将方法定义为不键入 abstract
时,编译器会说没问题。我知道抽象方法不能有主体。接口(interface)中某处的方法是否必须具有名称 abstract
? : 我的意思是,有什么区别:
public interface blabla {
public void aMethod();
//or
public abstract void aMethod();
}
最佳答案
不,将接口(interface)方法标记为 abstract
没有任何意义,也从不需要。
所有接口(interface)方法都是隐式抽象
(顺便说一句,也是public
)。
来自 JLS:
Every method declaration in the body of an interface is implicitly abstract, so its body is always represented by a semicolon, not a block.
Every method declaration in the body of an interface is implicitly public.
For compatibility with older versions of the Java platform, it is permitted but discouraged, as a matter of style, to redundantly specify the abstract modifier for methods declared in interfaces.
It is permitted, but strongly discouraged as a matter of style, to redundantly specify the public modifier for interface methods.
相关问题(+ 以历史引用的方式回答接口(interface)方法曾经需要abstract
的声明):
关于java - 是否必须抽象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9926964/