[英]Can a subclass access parent class private members if both the classes are in the same package
I am reading a Java Doc , which says 我正在阅读Java Doc ,其中说
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent.
子类继承其父级的所有公共成员和受保护成员,无论该子类位于哪个程序包中。如果该子类与其父级位于同一程序包中,则它还将继承父级的程序包私有成员。 You can use the inherited members as is, replace them, hide them, or supplement them with new members
您可以按原样使用继承的成员,替换它们,隐藏它们,或用新成员补充它们
It says a Subclass can access the Parent's class private members if they are in the same package. 它说如果子类在同一包中,则可以访问父类的私有成员。 I have tried it , I am unable to access the Private member of the parent Class in Subclass
我已经尝试过了,但是无法访问子类中父类的私有成员
No, it states 不,它说
If the subclass is in the same package as its parent, it also inherits the package-private members of the parent
如果子类与其父级位于同一包中,则它还将继承父级的package-private成员
package-private
(no modifier) is not the same thing as private
( private
modifier). package-private
(无修饰符)与private
( private
修饰符)不同。 See https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html for the differences. 有关差异,请参见https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html 。
There is a difference between package-private
and private
members. package-private
private
成员和private
成员之间有区别 。 Private members can't be accessed by anyone other than the class itself . 班级以外的任何人都不能访问私人成员。 But Package private members are those members who are
private
to that particular package only and they can be accessed by any member within that particular package . 但包私有成员谁是那些成员
private
只有特定的包,他们可以通过特定的包中的任何成员进行访问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.