简体   繁体   English

如果两个类都在同一包中,则子类可以访问父类的私有成员吗?

[英]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 (无修饰符)与privateprivate修饰符)不同。 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.

相关问题 为什么嵌套的子类可以访问其父类的私有成员,但是子孙却不能呢? - Why can nested child classes access private members of their parent class, but grandchildren cannot? 为什么外部 Java 类可以访问内部类私有成员? - Why can outer Java classes access inner class private members? 有什么方法可以在Java中定义一个私有类,只有同一包中的其他类才能访问它? - Is there any way to define a private class in Java which only other classes in the same package can access it? 公共嵌套类的子类无法访问封闭类的私有成员 - Subclass of a public nested class is unable to access private members of enclosing class 为什么不能在同一个包中调用另一个类的包私有方法呢? - Why can't I call a package-private method of another class that's a subclass of a class in the same package? 在Java嵌套类中,封闭类可以访问内部类的私有成员吗? - In Java nested classes, can the enclosing class access private members of inner classes? 内部类甚至可以访问外部类的私有成员。这是否侵犯了隐私? - Inner classes can access even the private members of the outer class.. doesn't it violates the privacy? @RequestMapping 不能在父 class 中使用 static 最终常量,该常量与子类不在同一个 package - @RequestMapping can not use a static final constant in parent class which is not in same package with subclass 包私有类中的公共成员 - Public members in package private class 是什么使您无法在包中声明其他类,从而无法访问包私有成员 - What prevents you declaring additional classes in a package, giving you access to package private members
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM