Access modifiers in Java are keywords that define where a class, method, or variable can be accessed. These modifiers help to protect data and control access to code. In this chapter, we will learn about the access modifiers and how they control the visibility of classes, methods, and variables.
Access modifiers are keywords that define the visibility or accessibility of classes, methods, constructors, and variables. They control where these members can be accessed from such as inside the same class, within the same package, or from other packages. By applying the access modifiers, we can restrict or allow access.
There are four types of access modifiers in Java:
Let us understand each access modifiers in details.
The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package. It has the widest scope among all other modifiers.
The following example demonstrates the use of public access modifiers.
Output:
Hello
The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
The following example demonstrates the use of private access modifiers.
When we compile the above program, it shows the following compile-time error
Main.java:8: error: data has private access in A
System.out.println(obj.data);//Compile Time Error
^
Main.java:9: error: msg() has private access in A
obj.msg();//Compile Time Error
^
2 errors
If you make any class constructor private, you cannot create an instance of that class from outside the class. For example:
When we compile the above program, it shows the following compile-time error.
Main.java:7: error: A() has private access in A
A obj=new A();//Compile Time Error
^
1 error
The protected access modifier is accessible within the package and outside the package, but through inheritance only. The protected access modifier can be applied to the data member, method and constructor. It cannot be applied to the class. It provides more accessibility than the default modifier.
The following example demonstrates the use of protected access modifiers.
Output:
Hello
When we do not specify any specifier, the default access specifier is prefixed by default. The default modifier is accessible within a package only. It cannot be accessed from outside the package. It provides more accessibility than private. But it is more restrictive than protected and public.
The following example demonstrates the use of default access modifiers.
Output:
Compile Time Error
In the above example, the scope of class A and its method msg() is default, so it cannot be accessed from outside the package. When we compile the above program, we get a compile-time error.
Access modifiers also affect method overriding in inheritance. When a subclass overrides a method from its parent class, the overridden method cannot have a more restrictive access modifier than the original method.
Follow the below given rules while using the access modifiers with the method overriding:
The following example demonstrates the access specifiers with method overloading.
The default modifier is more restrictive than protected. That is why there is a compile-time error.
The following table shows the accessibility or scope of Java access modifiers:
| Access Modifier | Within Class | Within Package | Outside Package by Subclass Only | Outside Package |
|---|---|---|---|---|
| Private | Y | N | N | N |
| Default | Y | Y | N | N |
| Protected | Y | Y | Y | N |
| Public | Y | Y | Y | Y |
We request you to subscribe our newsletter for upcoming updates.