Accessibility scope of Java access modifiers [duplicate]

扶醉桌前 提交于 2019-12-05 08:06:50

问题


Java has private, protected, and public access modifiers. Can you explain the accessibility scope of these modifiers.

How can I access a protected member within a different package?


回答1:


For better understanding you need to see this

Access Modifiers

                   Same Class      Same Package            Subclass     Other packages
public               Y                Y                      Y                   Y
protected            Y                Y                      Y                   N
no access modifier   Y                Y                      N                   N
private               Y               N                      N                   N


Here the important difference is between Default and protected.
Default: Never accessible outside the package
Protected: Only accessible outside the package, if and only if the class is a subclass
Please see this for further details.
Edit: As your question's answer is also the same that You can access the protected member by make your class a sub class of the class , in which protected member is defined



来源:https://stackoverflow.com/questions/16074621/accessibility-scope-of-java-access-modifiers

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