Why can't a class or an interface receive private or protected access modifiers?

后端 未结 3 1798
故里飘歌
故里飘歌 2020-12-12 22:19

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

3条回答
  •  执笔经年
    2020-12-12 23:06

    private means "only visible within the enclosing class".

    protected means "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 of protected. The second part of protected could apply, but it is covered by the default (package-protected) modifier, so protected is part meaningless and part redundant.

    Both private and protected can be (and frequently are) applied to nested classes and interfaces, just never top-level classes and interfaces.

提交回复
热议问题