且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么不能将类定义为受保护的?

更新时间:2022-10-15 17:12:22

因为没有意义。



受保护的类成员(方法或变量)就像package-private(默认可见性),除了它也可以从子类访问。

因为没有'subpackage'或'这样的概念Java中的package-inheritance',声明class protected或package-private是相同的。



你可以将嵌套和内部类声明为protected或private。 / p>

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.

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.