Witscale Test Center

3.2 Modifiers for access control > 3.2.1 The private modifier


3.2.1 The private modifier

The private modifier specifies the most restrictive access level. You can apply it to methods and member variables. The private methods and variable are accessible only within the declaring class[l1] [*****].  Therefore the members are usually declared private to hide the internals of a class from the other classes. A top-level class (a class that is not defined inside any other class) cannot be declared as private, as it would mean that the class cannot be instantiated and it would obviously unreasonable to declare a class that can never be accessible.

The best way to learn about access modifiers is through an example. Listing 3.1 illustrates how you can use the private modifier to restrict access. It shows two classes, the Cat and the Crocodile class. The Cat class has a private member variable secretAboutCat that cannot be accessed from any other class except the Cat class itself. Therefore, the Crocodile class in the following listing will fail to compile.


In this example, the Crocodile class cannot access the secretAboutCat variable as it is private to Cat class. If any such attempt is made, a compile-time error occurs saying something like, “The field secretAboutCat is not visible”.

Though private variables are inaccessible outside their class, it is sometimes necessary to accesss their value from outside. The correct way to do so is via accessor methods . Accessor methods are the public methods for getting a value or setting a new value of the variables. Since these methods are public (we will see this access modifier shortly), other classes can invoke them. For instance, you can read secretAboutCat from outside the Cat class by writing a public accessor method getSecretAboutCat() in the following manner:

 

public String getSecretAboutCat() {

   return secretAboutCat;

}

 

Thus by declaring member variables as private, you can ensure that they can be accessed (if at all) only through the proper way via accessors. In other words, no other class can directly get/modify the value of private member variable thus ensuring encapsulation[†††††].

Similar to the private variables, the methods defined as private are also inaccessible to the outside world. Since no other class can use them, you can change the implementation of such methods without affecting any other classes. You should declare methods as private when they are either implementing the internal functionality for the class and/or when they are not useful to the other class.

 

The accessibility of member methods and variables is same. For simplicity, we will discuss access modifiers with examples trying to access only member variables. You can try out the same examples by invoking methods (instead of accessing variables). Note that the accessing rules are same for methods as well.

 

Understanding accessibility with sample class hierarchy

Figure 3.1 explains the private access with the sample class hierarchy for Animal class. We will use this hierarchy to explain all the access modifiers in this chapter. .To better understand the scope of access modifiers, assume that some classes from this hierarchy reside in package com.example.pets whereas the remaining classes reside in the default package. You can see the classes that have the access with dark-gray color[l2]  and classes that do not have access with a light gray color. Since the private variables are accessible only to the class itself, the figure 3.1 shows only the Cat class in dark-gray color.


 

 

Figure 3.1 Only the Cat class has access its own private members

 

Note that no other class can access the private variables or methods of class Cat expect the Cat class itself. Hence, all other classes are shown with a light-gray color indicating that they can not directly access variables such as secretAboutCat from Cat class.