为什么一个类不能被定义为protected? [英] why a class cannot be defined as protected?
问题描述
我知道这是一个愚蠢的问题,但仍然有一个疑问,需要清除。
我的问题是为什么我们不能将类定义为protected。
我知道我们不能为什么?应该有一些具体的原因。
因为没有意义。
受保护的类成员(方法或变量)就像package-private(默认可见性),除了它也可以从子类访问。
因为没有'subpackage'或' package-inheritance'in Java,声明类protected或package-private将是同样的事情。
你可以声明嵌套和内部类为protected或private。 / p>
I know this is a stupid question, but still i have a doubt which needs to be cleared.
My question is why cannot we define a class as protected.
I know we cannot 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.
这篇关于为什么一个类不能被定义为protected?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!