5

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?

Tharif
  • 13,794
  • 9
  • 55
  • 77
  • 1
    FInd here [http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html](http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) – Subhrajyoti Majumder Apr 18 '13 at 04:43
  • http://stackoverflow.com/questions/15939002/protected-access-modifier-in-java – jamesmortensen Apr 18 '13 at 04:46
  • The table in the official tutorial (which is copied in the accepted answer) isn't super pedagogical imo. Have a look at [this table](http://stackoverflow.com/a/33627846/276052) for a slightly better visualization. – aioobe Nov 13 '15 at 12:53

1 Answers1

21

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.
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

Freak
  • 6,786
  • 5
  • 36
  • 54