Visibility Modifiers in Java
Introduction
Visibility modifiers are a concept of object-oriented programming (OOP) that restrict the access of certain class members to other classes. This concept is essential in OOP because it allows for the developing of robust and secure software applications. By controlling which classes or objects can access or modify certain variables or methods, developers can control an application's behaviour and data flow.
Java’s most common visibility modifiers are public, private, protected, and package-private. Each of these modifiers has a unique purpose and can be used to control the access and visibility of class members. Understanding the differences between these modifiers and how to use them is essential for any Java programmer.
Visibility modifiers in Java are used to restrict the access of classes, variables, methods and constructors to other classes and packages. Java has four different types of visibility modifiers which are listed below:
1. Public: A public modifier allows a class, method, constructor or field to be accessed by any class or package. This is the most visible form of access control.
2. Protected: A protected modifier allows a class, method, constructor or field to be accessed by classes in the same package and subclasses in other packages.
3. Default (Package-private): A default modifier allows a class, method, constructor or field to be accessed by classes in the same package only.
4. Private: A private modifier allows a class, method, constructor or field to be accessed by the class only.
When no visibility modifier is specified, the default modifier is applied. It is also important to note that a class cannot be declared as private or protected, only public or default.
Public
The public is the most permissive visibility modifier in Java. Any class member declared public can be accessed by any other class, regardless of where the class is located in the application. This makes the public the most common visibility modifier used in Java applications.
Any other class can access public class members in the application, including classes in different packages. This means that public class members are accessible to all classes, regardless of their location in the application.
Private
Private is the most restrictive visibility modifier in Java. Any class member declared private can only be accessed by the class that declared it. This means that no other classes can access the private class member, even if the class is in the same package.
Private class members are often used to store only relevant information to the class that declares it. For example, a class may have private variables that store data used only within the class.
Program
class Private{
private int n=40;
private void dis()
{
System.out.println("Hello java");
}
}
public class PrivateModifier{
public static void main(String args[]){
Private o1 = new Private();
System.out.println(o1.n);//Compile Time Error
o1.dis();//Compile Time Error
}
}
Output
PrivateModifier.java:13: error: n has private access in Private
System.out.println(o1.n);//Compile Time Error
PrivateModifier .java:14: error: dis() has private access in Private
o1.dis();//Compile Time Error
Protected
Protected is the second most restrictive visibility modifier in Java. Any class member declared protected can only be accessed by the class that declared it and any subclasses of that class. This means that a protected class member can be accessed by any subclass, even if the subclass is located in a different package.
Protected class members often store information relevant to a class and its subclasses. For example, a class may have protected variables that store data used by the class and its subclasses.
Package-Private
The package-private visibility modifier is the second most permissive visibility modifier in Java. Any class member declared package-private can be accessed by any class located in the same package as the class that declared it. This means that a package-private class member can be accessed by any class in the same package, regardless of its location within the application.
Package-private class members often store information relevant to a specific package. For example, a package may have package-private variables that store data that is only relevant to the package.
Conclusion
Visibility modifiers are an essential concept of object-oriented programming used to control class members' access and visibility. Java’s most common visibility modifiers are public, private, protected, and package-private. Each of these modifiers has a unique purpose and can be used to control the access and visibility of class members. Understanding the differences between these modifiers and how to use them is essential for any Java programmer.