Access control modifiers let you specify what types (mainly classes) and members of those types are accessible from other types and members, in order to get a proper encapsulation. The following table summarizes the meaning of the four (really three) available access control modifiers in Java. You can read the content of it as “A member having the modifier X is accessible from…”:

Class Package1 Subclass 2 Everywhere Accessible from…
public Everywhere
protected 3 Package and kids
Default (nothing) Package
private Same class

The following example shows two classes in the same package. Class B extends class A and so it can access all the fields of the superclass except the private ones:

Now take a look at the following example showing a third class C also extending A. However class C belongs to a different package than class A and so it can only access public and protected fields of the superclass:


Leave a Reply