Why can a class not be defined as protected?

By | August 1, 2019

Question

I know this is a stupid question, but I still have a doubt which needs to be cleared.

My question is, why can we not define a class as protected?

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

Solution

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.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *