java - 是否必须抽象?

标签 java interface

如您所知,在 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/

相关文章:

java - 在 Java 8 中使用 .class 进行转换?

go - 有没有一种方法可以通用地表示一组相似的函数?

java - Spring Boot 序列化 JSON

java - Spring @Transactional DAO 调用返回相同的对象

.NET TypeBuilder - VerificationException : Operation could destabilize the runtime

interface - 实现内部非静态接口(interface)

c# - OO 设计 - 通过接口(interface)公开实现细节

.net - 如何对 IList<T> 执行二分查找?

Java:何时使用字段与参数?

java - 使用 'PlatformUI.getWorkbench().restart()' 重新启动 Eclipse 不会重新启动 RCP 产品