You control the access to a class using access modifiers, which define different levels of access for class members. For example, you might want the member variables for a class to only be accessible to derived classes. You must declare an access modifier before the type of a member variable, the return type of a method, or the definition of a class. Java supports four different access modifiers: public, protected, default, private. Some of these modifiers apply to classes, some to class members, and some to both:
Using Access Modifiers in Java
Here is an example of access modifiers in action:
public class Circle {
private double radius;
private double area;
public Circle(double radius) {
setRadius(radius);
}
public double getArea() {
return area;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
area = Math.PI * radius * radius;
}
}
Assume that area represents a value that is very time consuming to calculate and that users of the Circle class access the area of a circle far more often than they set the radius. With these assumptions in mind, the Circle class has been designed so that the area of a circle will be calculated whenever the radius is set, rather than each time the getArea() method is invoked. By using the access modifier private with the instance variables radius and area, access to these values is only available through the methods getArea(), getRadius(), and setRadius(), thus insuring that the area and radius of a circle are always consistent.
Keywords public and private are member access modifiers. Instance variables or methods declared with member access modifier public are accessible wherever the program has a reference to a Time1 object. Instance variables or methods declared with
member access modifier private are accessible only to methods of the class in which they are defined. Every instance variable or method definition should be preceded by a member access modifier.
Methods tend to fall into a number of different categories: methods that get the values of private instance variables; methods that set the values of private instance variables; methods that implement the services of the class; and methods that perform various mechanical chores for the class, such as initializing class objects, assigning class objects, and converting between classes and built-in types or between classes and other classes.
Access methods can read or display data. Another common use for access methods is to test whether a condition is true or false and such methods are often called predicate methods[1]. An example of a predicate method would be an isEmpty method for any container class (a class capable of holding many objects) such as a linked list, a stack or a queue. A program might test isEmpty before attempting to read another item from the container object. A program might test isFull before attempting to insert another item into the container object.
This series of images shows how the different access modifiers apply to a group of classes.
The frames demonstrate accessibility with respect to the Dog class:
[1]Predicate: Predicate in general meaning is a statement about something that is either true or false. In programming, predicates represent single argument functions that return a boolean value.