java - 如果两个类都在同一个包中,子类可以访问父类私有(private)成员吗

标签 java inheritance

我正在阅读 a 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

它说子类可以访问父类的私有(private)成员,如果它们在同一个包中。我试过了,我无法访问子类中父类的私有(private)成员

最佳答案

不,它说

If the subclass is in the same package as its parent, it also inherits the package-private members of the parent

package-private(无修饰符)与 private(private 修饰符)不同。参见 https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html对于差异。

关于java - 如果两个类都在同一个包中,子类可以访问父类私有(private)成员吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46889987/

相关文章:

java - 异常 NullPointer,建议?

java - java子类如何继承受访问保护的父类字段?

java - 避免在 Java 中重复导入 : Inherit imports?

java - 创建aar文件并进行检查

java - 如何运行 JAR 文件

java - 使用 ProcessBuilder 重新启动后 Swing 应用程序变慢

java - 与两种不同实现的接口(interface)

java - 是否可以使用java程序从数据透视表中读取数据?

java - 如何从弹出窗口中检索 EditText 值?

c# - C#创建派生类时,是否可以用n参数函数覆盖0参数虚函数?