Controlling Access to Class Members
- Access level modifiers determine whether other classes can use a particular field or invoke a particular method.
- There are two levels of access control:
- At the top level – public, or package-private (no explicit modifier).
- At the member level – public, private, protected, or package-private (no explicit modifier).
- A class may be declared with the modifier public, in which case that class is visible to all classes everywhere.
- If a class has no modifier (the default, also known as package-private), it is visible only within its own package.
- At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning.
- For members, there are two additional access modifiers: private and protected.
- The private modifier specifies that the member can only be accessed in its own class.
- The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
| Modifier | Class | Package | Subclass | World |
| public | Y | Y | Y | Y |
| protected | Y | Y | Y | N |
| no modifier | Y | Y | N | N |
| private | Y | N | N | N |
Tips on Choosing an Access Level
- If other programmers use your class, you want to ensure that errors from misuse cannot happen.
- Access levels can help you do this.
- Use the most restrictive access level that makes sense for a particular member. Use private unless you have a good reason not to.
- Avoid public fields except for constants.
Encapsulation
- Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
- In encapsulation, the variables of a class will be hidden from other classes and can be accessed only through the methods of their current class.
- Therefore, it is also known as data hiding.
Properly encapsulated
To achieve encapsulation in Java
- Declare the variables of a class as private.
- Provide public setter and getter methods to modify and view the variables values.
public class Box {
private double width;
private double height;
private double depth;
public Box(double width, double height, double depth) {
this.width = width;
this.height = height;
this.depth = depth;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getDepth() {
return depth;
}
public void setDepth(double depth) {
this.depth = depth;
}
// compute and return volume
double volume() {
return width * height * depth;
}
// sets dimensions of box
void setDim(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
}
- The public setXXX() and getXXX() methods are the access points of the instance variables of the EncapTest class.
- Normally, these methods are referred as getters and setters.
- Therefore, any class that wants to access the variables should access them through these getters and setters.
Packages
- Packages are containers for classes.
- Packages are used to keep the class name space compartmentalized.
- Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.
- The package is both a naming and a visibility control mechanism.
- Define classes inside a package that are not accessible by code outside that package.
- Define class members that are exposed only to other members of the same package.
- The package statement syntax.
package <top_pkg_name>[.<sub_pkg_name>];
Packages and Member Access
- Packages act as containers for classes and other subordinate packages.
- Classes act as containers for data and code.
- Java addresses four categories of visibility for class members.
- Subclasses in the same package.
- Non-subclasses in the same package.
- Subclasses in different packages.
- Classes that are neither in the same package nor subclasses.
Understanding Protected Members
| private | default | protected | private | |
| same class | Y | Y | Y | Y |
| subclass same package | N | Y | Y | Y |
| non-subclass same package | N | Y | Y | Y |
| subclass different package | N | N | Y | Y |
| non-subclass different package | N | N | N | Y |
Importing Packages
- Java includes the import statement to bring certain classes, or entire packages, into visibility.
- In a Java source file, import statement occur immediately after the package statement, and before any class definitions.
- The import statement syntax.
import pkg1 [.pkg2].(classname | *);
Java’s Class Library Is Contained in Packages
- All the standard Java classes included with Java are stored in a package called java.
- The basic language functions are stored in a package inside of the java package called java.lang.
- The java.lang package is imported implicitly by the compiler for all programs.