Java – why can’t the interface be final?
•
Java
JLS 2.13. 1 interface modifier
If I can write and create static inner classes in interface I and provide implementations in them, why is there such a limitation
interface Type { // Normal class Value { private Value() { } public void print() { System.out.println("Test"); } } public final Value value = new Value(); }
Solution
Incidentally, nested classes are not available when this restriction is first defined, so the problem may be why this restriction is not lifted
The last class cannot have any sub classes The method of using interfaces only to define subclasses is considered a best practice, so the two are contradictory
You can use interfaces for other things
>Comments > JavaDocs > constants > define nested classes only
But these are incidental purposes of the interface
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码