JLS 2.13.1 接口(interface)修饰符
An interface cannot be final, because the implementation of such a class could never be completed.
如果我可以在接口(interface)中编写创建静态内部类,我可以在其中提供实现,那么为什么会有这样的限制
interface Type {
// Normal
class Value {
private Value() {
}
public void print() {
System.out.println("Test");
}
}
public final Value value = new Value();
}
最佳答案
在接口(interface)中,您根本无法提供任何形式的实现:甚至静态方法也不行。将任何方法设为 final 没有意义,因为它们尚未实现。
代码示例:
假设我有一个名为IExample
的接口(interface)及其具体实现Example
:
interface IExample{
public final void run();
}
class Example implements IExample{
// wait! I can't override because it's final! but it's yet to be implemented?!
public void run(){
}
}
关于java - 为什么接口(interface)不能是最终的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12351646/