Java Access specifiers
Java comes with four access specifiers namely, public, private, protected, and default. The details are as follows:
The public access specifier is the least restrictive of all access specifiers. A field, method, or class declared public is visible to any class in a java application in the same package or in another package.
The private access specifier is the most restrictive of all access specifiers. The private access specifier cannot be used for classes and interfaces as well as fields and methods of an interface. Fields and methods declared private cannot be accessed from outside the enclosing class. A standard convention is to declare all fields private and provide public accessor methods to access them. Thus when data is important, sensitive, and con not be shared with others, it is declared as private.
The protected access specifier is used with classes that share a parent-child relationship which is referred to as inheritance. The protected keyword cannot be used for classes and interfaces as well as fields and methods of an interface. Fields and methods declared protected in a parent or super class can be accessed only by its child or subclass in another packages. However, classes in the same package can also access protected fields and methods, even if they are not a subclass of the protected member’s class.
The default access specifier is used when no access specifier is present. The default specifier gets applied to any class, field, or method for which no access specifier has been mentioned. With default specifier, the class, field, or method is accessible only to the classes of the same package. The default specifier is not used for fields and methods within an interface.
Figure: Using Access Specifiers
Class A with four data members having public protected, default, and private access specifiers. Class B is a child class of A and class C is another class belonging to the same package, Package1. From the figure it is clear that they belong to the same package.
The package, package2 consists of classes D and E. Class D can only access the public members of class A. However, class E can access public as well as protected members of class A even though it belongs to another package. This is because class E is a child class of A. However, none of the classes B,C,D, or E can access the private members of class A.
Access Specifier |
Class |
Package |
Subclass |
World |
public |
Y |
Y |
Y |
Y |
Protected |
Y |
Y |
Y |
N |
No modifier (default) |
Y |
Y |
N |
N |
Private |
Y |
N |
N |
N |
Table: Access Levels of Access Specifiers
The first column states whether the class itself has access to its own data members. As can be seen, class can always access its own members. The second column states whether classes within the same package as the owner class (irrespective of their parentage) can access the member. As can be seen, members can be accessed except private members.
The third column states whether the subclasses of a class declared outside this package can access a member. In such cases, public and protected members can be accessed. The fourth column states whether all classes can access a data member.