Access Control Modifiers in Java

Below is a table that quickly shows you the difference between the access modifiers Public, Protected, Package (default), and Private.

The topmost row with Class, Package, Subclass, and World refer to the amount of access allowed.

The leftmost column with public, protected, no modifier (also known as default or package), and private refer to the keyword modifiers that can be used.

            | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |   +   |    +    |    +     |   +
————————————+———————+—————————+——————————+———————
protected   |   +   |    +    |    +     |
————————————+———————+—————————+——————————+———————
no modifier |   +   |    +    |          |
————————————+———————+—————————+——————————+———————
private     |   +   |         |          |

+ : accessible
  : not accessible

So the public access modifier allows access everywhere.

The protected access modifier allows access only within the subclass, but any package.

No modifier allows access only within the subclass, and only from the same package.

The private access modifier only allows access from within the class.

[Source]

Related: Protected

What's Your Opinion?