Java provides four levels of protection for methods and instance variables: public, private, protected and package. Before applying protection levels to a program, one should know what each form means and understand the fundamental relationships that a method or variable within a class can have to the other classes in the system.
Package Methods and variables with package protection are visible to all other classes in the same package but not outside that package. Most of the time, the programmer needs to be more explicit when protection is defined for methods and variables of a class. Package protection is the default level of protection. Package protection is not an explicit modifier; it can be added to the definition of a method or variable. Private The keyword private limits the visibility of methods and instance variables to the class in which they are defined. For example, private instance variable can be used by methods inside the same class but cannot be seen or used by any other class or object. In addition, neither private variables nor private methods are inherited by sub-classes.
Private protection ensures that methods and variables are accessible only to other methods in the same class. To create a private method or instance variable, add the private modifier to its definition as shown in Program
Implementing private access specifier for variables and methods.
class PrivateClass { private String name = "Java"; private int age = 20; public void show() { System.out.println("Name" +name); System.out.println ("Age"+age); } private void show1() { System.out.println(" Name" +name); System.out.println("Age"+age); } } class Private Demo { public static void main(String args[]) { PrivateClass p1 = new PrivateClass(); p1.show(); //accessible //p1.show(); //not accessible as show1() is declared as private method } }
The output of Program is given below:
Name Java Age 20
Though the variables Name and Age in Program are declared as private, all instance variables are private by default. Here, the method show() is public and hence it can be accessed from the method main(); however, if we include p1.show1() (as mentioned in the last comment line) in the program, compilation of the program will give an error because the method show1() is private and hence cannot be accessed from the method main()·
Public The opposite of private, and the least restrictive form of protection, is public. A method or variable that is declared with the modifier public is accessible to the class in which it is defined and all the sub-classes of that class, as well as all the classes in the package and any other classes outside that package.
In many ways, public is similar to the default package. Both allow methods and variables to be accessed by other classes in the same package. The difference occurs when the package is created. Variables and methods with package protection can be used in classes that exist in the same package. But if the class is imported from outside the package, those methods and variables will not be accessible unless they have been declared public. The syntax for declaring a variable or method public is as shown below:
public Add() {}
Protected The final form of protection available in Java concerns the relationship between a class and its present and future sub-classes declared inside or outside a package. These subclasses are much closer to a particular class than to any other ‘outside’ classes, for the following reasons:
• Sub-classes usually ‘know’ more about the internal implementation of a super-class.
• Sub-classes are often written by a reliable programmer who knows the source code.
• Sub-classes are frequently modified as to enhance the representation of the data within a parent class.
To support a special level of visibility reserved for sub-classes that are required to be somewhat less restricted than private, Java offers an intermediate level of access between package and private called, protected.. The keyword protected implies that those methods and variables are accessible to all classes inside the package and only sub-classes outside the package.
A summary of types of protection The differences between various types of protection can be unclear particularly in the case of protected methods and variables. Tabledescribes different types of protection (access Specifiers).
Table Different types of protection.
Visibility | Public | Protected | Package (No modifier) | Private |
From the same class | Y | Y | Y | Y |
From any class in the same package | Y | Y | Y | N |
From any class outside the package | Y | N | N | N |
From a sub-class in the same package | Y | Y | Y | N |
From a sub-class outside the same package |
Y |
Y |
N |
N |