Why can a class not be defined as protected?

后端 未结 12 2136
长发绾君心
长发绾君心 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:54

    Protected : VISIBLE only to package level*.

    class is defined protected ---> it cannot be extended from outside package(not visible).

    And if it cannot be extended then it is meaningless to keep it as protected, because then it will become default access which is allowed.

    Same applies to private defined classes.

    Note : Nested or inner classes can be defined protected or private.

    * : Explore protected keyword, for this answer I made it succinct.

提交回复
热议问题