I am reading some Java text and the text says that we can only apply public or default access modifier for class and interface. Therefore, it is a compiling error if we declare:
private class A {}
or
protected class A{}
I am just curious why a class or an interface cannot receive private or protected access modifiers?
privatemeans “only visible within the enclosing class”.protectedmeans “only visible within the enclosing class and any subclasses, and also anywhere in the enclosing class’s package”.private, therefore, has no meaning when applied to a top-level class; the same goes for the first part of the definition ofprotected. The second part ofprotectedcould apply, but it is covered by the default (package-protected) modifier, soprotectedis part meaningless and part redundant.Both
privateandprotectedcan be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.