Public, protected, default and private in Java

Here is some simple rules to memorize these access modifiers :

  • private: class scope.
  • default (or package-private): package scope
  • protected: package scope + child (like pakage, but we can subclass it from different pakages). The protected modifier always keeps the “parent-child” relationship.
  • public : everywhere.

As a result, if we divide access right into three rights:

  • (D)irect (invoke from a method inside the same class)
  • (R)eference (invoke a method using a reference to the class, or via “dot” syntax)
  • (I)nheritance (via subclassing)

then we have this simple table:

enter image description here

This entry was posted in Java and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s