Scope of protected members

旧街凉风 提交于 2019-12-02 01:34:25

You can't even access Parentclass.x in Childclass because x has default visibility (not protected). See http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

edit:

x.child.Friendclass is not in the same package as x.parent.Parentclass. x.child.Friendclass does not inherit from x.parent.Parentclass.

as TotalFrickinRockstarFromMars's summary states and the Java access control docs also state, this means that Friendclass is not allowed to access the field x.

corsiKa

There are four access modifiers

private - just this class
no modifier - just this class or this package (NOT subclass)
protected - just this class, this package, or subclass
public - everyone and their cousin

Since it uses the default modifier, it has access if one of the following is true:

  1. Is part of the class itself (Nope!)
  2. Is part of the package of the class itself (Nope!)

So it fails the criteria, and so you don't get access.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!