Access Specifiers


We have quite often been using the access specifiers- public and private. We shall now see what these access specifiers actually are, to what entities they can be applied and what access restrictions are imposed by their usage.
But before looking into access specifiers, let us see what packages are. Packages, in a simple language can be stated to be a group of related classes. They are just like folders on your computer and the classes within these packages are like the files contained in folders. Packages help in organising classes. We shall see how to create and use packages later on. Every class is a part of a single package. When we do not explicitly specify to which package a class belongs, it is placed in the default package. Before we can use a class from a package, we need to import either the entire package or that particular class by using the import statement as we have done when using the Scanner class. Two packages are implicitly imported into a class. One is the java.lang package and the other is the default package in which we are working. That was the reason why we were able to use the Student class in the StudentTest class without importing the Student class. The class String which we often use to store text is a part of the java.lang package.
Access specifiers are used to control the visibility of members like classes, variables and methods. There are three access specifiers: public, private and protected. We shall see only about the public and private access specifiers for now. Protected access specifier comes into picture when inheritance is implemented. Also, we may leave the access specifier keyword unspecified just as we had seen when learning about classes in the beginning. For example, the following class declaration is also valid though no access pacifier is stated.
class Student {
}
In such cases, the default restrictions (also known as the package restrictions) are applied. Now, we will see what effect these specifiers have on classes, methods and variables. Before moving further, there is another thing to be noted. A simple program file can contain more than one class but only one of them should be declared as public. If more than one class is declared in the same program file, both of them will be considered as different classes. Or in other words, one class is not said to be contained within the other class. This is important to be noted to avoid any erroneous interpretation of the restrictions imposed by access specifiers.
Access specifiers for classes
When a class is declared as public, it is accessible to other classes defined in the same package as well as those defined in other packages. This is the most commonly used specifier for classes. A class cannot be declared as private. If no access specifier is stated, the default access restrictions will be applied. The class will be accessible to other classes in the same package but will be inaccessible to classes outside the package. When we say that a class is inaccessible, it simply means that we cannot create an object of that class or declare a variable of that class type. The protected access specifier too cannot be applied to a class.
Access specifiers for instance variables
Accessibility of instance variables can be controlled with all the four access specifiers- public, private, protected and the default restriction (no access specifier mentioned). When a variable is declared as public, it can be modified from all classes in the same or different packages. When we talk of modification here, it refers to the modification of the variables corresponding to an object built out of this class and not the variable of the class itself. If no access specifier is stated, then these variables are accessible from classes in the same package only. If the variable is private, access is restricted to the class itself. If we create an object of this class in the main method of the same class, the variable is accessible but if the object is created in different class, the variable is accessible. Look at the following sample code for example.
public class Student {

Private name;

Public static void main ( String[] args ) {
Student s = new Student ();
s.name = “Sai”; // allowed
}
}

In the above program, we were able to modify the name variable directly even though the access specifier was private. This is because the variable was accessed from the same class itself. If we create an object of the Student class in a new class, such an StudentTest, access would be denied.
public class StudentTest {

Public static void main ( String[] args ) {
Student s = new Student ();
s.name = “Sai”; // not allowed
}
}

Access specifier for methods
Access specifiers for methods work in a similar way to that of variables. Methods too can be defined using all four levels of access protection.
Access specifiers for constructors
All four access restriction may be applied to constructors. When we talk of accessing a constructors, it refers to creating an object of that class using that particular constructor. If a constructor is declared as public, its objects can be created from any other class. If no access specifier is stated, its objects may be created from classes belonging to the same package only. If the private access specifier is applied, objects may be created in that particular class only. However, we generally do not apply the private access specifiers to a constructor if the class contains only a single constructors as this may render the class unusable. Look at the following code where a constructor is declared as private. We can create objects within the same class only. In this particular example, we have created the object in the main method of the class. Attempting to create the object in different class would result in compilation errors.
public class Student {

Public name;

Private Student() {
}

Public static void main ( String[] args ) {
Student s = new Student (); // allowed
}
}

public class StudentTest {
public static void main ( String[] args ) {
Student s = new Student (); // not allowed
}
}
Following table shows what access specifiers may be assigned to different elements. Note that all four levels may be applied to all elements except classes. Classes may be declared with only public and private access specifiers
public private protected < unspecified >
class allowed not allowed not allowed allowed
constructor allowed allowed allowed allowed
variable allowed allowed allowed allowed
method allowed allowed allowed allowed
The following table summarises the above said points regarding the accessibility of entities. We haven’t looked into what a subclass is and the accessibility in a subclass. We shall see about it when we deal with inheritance.
class subclass package outside
private allowed not allowed not allowed not allowed
protected allowed allowed allowed not allowed
public allowed allowed allowed allowed
< unspecified > allowed not allowed allowed not allowed
Author: , 0000-00-00