|

With the knowledge of how each access modifier work, let us summarize by
comparing them with each other. The private is the most restrictive of them and
grants access only to the declaring class. The default is little bit
more liberal than private. It grants access to the declaring class just like
the private does. In addition, it grants access to each class in the package of
the declaring class. The protected is even more liberal than default. In
addition to the access that the private and default access grants (the
declaring class and all classes in its package), it also gives access to all
the subclasses of the declaring class through inheritance. The public is all pervasive
giving access to all classes irrespective of their package. Figure 3.5
illustrates four access levels and how they relate to each other.
Figure 3.5 Comparing the access levels from most restrictive (private) to least restrictive (public)
The symbols for class, package and class-hierarchy are used to denote the access to the declaring class, classes in the same package as the declaring class and all subclasses respectively.
|
|
Figure 3.5 uses co-centered rectangles to represent the relative restrictiveness of access levels. As you can see, the rectangle representing default access level is encompassing the rectangle representing private access, it means that default is liberal than private. Similarly it shows how protected is more liberal than default and so on. When we say “protected is more liberal than default”, it means the protected level grants all the accesses that default level would grant in addition to its own access. |
Besides access modifiers, there are “other modifiers” that you can use to modify the implementations or storage characteristics of class and/or its members. In the following sections we will study these modifiers.