Why can a class not be defined as protected?

后端 未结 12 2141
长发绾君心
长发绾君心 2020-12-04 08:03

Why can we not define a class as protected?

I know that we can\'t, but why? There should be some specific reason.

12条回答
  •  盖世英雄少女心
    2020-12-04 08:35

    Because it makes no sense.

    Protected class member (method or variable) is just like package-private (default visibility), except that it also can be accessed from subclasses.
    Since there's no such concept as 'subpackage' or 'package-inheritance' in Java, declaring class protected or package-private would be the same thing.

    You can declare nested and inner classes as protected or private, though.

提交回复
热议问题