Access/visibility modifiers control access to a class, interface, data member or method. There are four levels of visibility in Java:
- public
- protected
- package
- private
1. Access/Visibility Modifiers for Top-Level Classes.
Visibility of a top-level class can be either public or package.
public
The keyword public is the only modifier, which can be used with a top-level class. If a class is to
be visible to all the classes irrespective of their package, then it must be declared as public by specifying the modifier public, which should appear before the keyword class.
package
There is no keyword package for visibility control. In the absence of any access/visibility modifier before the top-level class, its visibility is only within the package in which it is defined. The concept of package is somewhat similar to the concept of friend classes in C++.
2. Access/Visibility Modifiers for Data Members and Methods.
When used in the variable or method declarations, access/visibility modifiers control access to the variable or method i.e. they decide who can access the variables and methods. These modifiers are not applicable for local variables as their visibility/scope is anyhow limited to the method/block in which they are declared.
public
Any method or variable is always visible in the class in which it is declared. The instance methods of a class can access any other method (instance as well as static method) or variable (instance as well as static variable) declared in the class. Similarly a static method of a class can access any other static method or static variable declared in the same class.
If a method or variable is to be visible to all the classes, then it must be declared as public by specifying the modifier public, which should appear before the data type. For example, main() method is always declared as public. The reason is that main() method is accessed by the code which is part of JVM i.e. it is outside the class in which main() method is declared. Similarly an instance or static variable can be declared public by specifying modifier public as shown below:
public float width;
public static float width;
A variable or method declared as public has the widest possible visibility. It can be accessed from any class.
private
If you want that a method or variable should not be visible outside the class in which it is declared then its access modifier should be private. We use this modifier to hide the variable or method so that it cannot be accessed from outside the class.
A variable declared as private has the least possible visibility but it is the most commonly used modifier to encourage data encapsulation and data hiding.
package
There is no keyword package. In the absence of any access/visibility modifier before a data member or method, its visibility is only within the package in which it is defined.
protected
A variable declared as protected can be accessed from all the classes belonging to the same package as that of the class in which member is declared. This visibility is similar to the package scope. But in addition to this a protected member can be accessed from any child class irrespective of the package in which it is declared.
Example: A simple class Box is defined below.
1 class Box
2 {
3 double width;
4 double height;
5 double length;
6 }
Example: The class Box is redefined here to include one method volume().
1 class Box
2 {
3 double width;
4 double height;
5 double length;
6 void volume()
7 {
8 double vol = width * height * length;
9 System.out.println(vol);
10 }
11 }