In Java, Modifiers are keywords that change the meaning of definitions when they are added to them. These modifiers are used to control the access of the definitions and also provide the class functionality-related information to the JVM. Choosing the right modifier is vital. It can be the deciding factor of whether you successfully compile a Java program or end up with a compilation error, forcing you to go back and fix the code. There are two types of modifiers, Access Modifiers and Non-Access Modifiers in Java. In this blog, we’ll discuss various Access Modifiers in Java and the similarities and differences between each of them.
In this article, let us look:
Access Modifiers are keywords used to set visibility for classes, interfaces, methods, data members, and variables. In simple words, the scope of Access Modifiers in Java controls the mentioned features’ access levels. They are also referred to as Java Access Specifiers. Now that we have an understanding of Access Modifiers in Java, let’s have a look at its usage.
Using Access Modifiers in Java programs, a particular class method or variable can be restricted to access, or it can be hidden from the other classes.
The four different Access Modifiers in Java are:
The visibility of each type of Access Modifiers in Java can be represented in a tabular form. This will help to understand the similarities and differences between Protected and Default Access Modifiers in Java as well as Public and Private Access Modifiers in Java.
For a better understanding, let’s have a look at the different types of Access Modifiers in Java with examples.
If no Access Modifier is defined, then it is considered as a Default Access Modifier. The modifier is only accessible within the same package and cannot be accessed from outside. Default Access Modifiers provide more visibility than Private Access Modifiers but have more restrictions than Protected and Private Access Modifiers.
Default Access Modifiers in Java with example program:
package defaultPackage;
class MyClass {
public static void main(String[] args) {
System.out.println(“Hello World”);
}
Here, there is no modifier specified for the class (MyClass), and hence, is treated as the Default Access Modifier. The class is visible to all the other classes that belong to the defaultPackage package. However, we will get a compilation error if we use the class (MyClass) outside the package.
The methods or variables declared using a Private Access Modifier are visible within the same class only. Even a class of the same package won’t access the members defined by the private Access Modifiers. It is the most restrictive access level in Java. They cannot be used for classes and interfaces. The keyword ‘ private’ is used to define the modifier.
Private Access Modifiers in Java with example program:
class NUMBERS{
private double num = 100;
private int square(int x){
return x*x;
public class Test {
public static void main(String args[]){
NUMBERS obj = new NUMBERS();
System.out.println(obj.num);
System.out.println(obj.square(5));
In this example, we will get a compilation error as the output is a private data member, and the method is only available within the class NUMBERS and cannot be accessed outside of it.
We can access the methods and data members within the same package or subclasses in different packages with Protected Access Modifiers in Java.
This method cannot be applied to classes and interfaces. It provides more accessibility than the Default and Private Access Modifiers. For instance, the visibility of Protected Access Modifiers for a class in Java is the same as that of Private Modifiers. However, the same package subclass’s visibility is more for Protected Access Modifiers than Private Access Modifiers. The keyword ‘protected’ is used to define a Protected Access Modifier.
Protected Access Modifiers in Java with example program:
package j1;
public class A
{
protected void display()
System.out.println(“Jigsaw Academy”);
package j2;
import j1.*;
class B extends A
public static void main(String args[])
B obj = new B();
obj.display();
In this example, two packages, j1 and j2, are created. Class A in the package p1 is made public, to access it in package j2. The method displayed in class A is protected, and class B is inherited from class A, and this protected method is then accessed by creating an object of class B. The output of the program will be “Jigsaw Academy.”
There isn’t any restriction on the classes, methods, and data members defined by the Public Access Modifiers. They can be accessed from anywhere. The keyword ‘public’ is used to define the Public Access Modifier.
Public Access Modifiers in Java with example program:
package s1;
public void display()
package s2;
import s1.*;
class B
A obj = new A;
In this example, two packages, s1 and s2, are created. The method from Class A can be accessed without any restrictions by Class B, as a public Access Modifier defines it.
Summing up the key points of Access Modifiers in Java:
If you want to learn more about Access Modifiers in Java or master Java language, you can consider enrolling for Jigsaw Academy’s Master Certificate In Full Stack Development offered by Jigsaw Academy. This live instructor-led online program is 170-hour-long and extensively covers Full Stack Development with Automation and AWS Cloud.
What is the Collection Framework in Java?
Multithreading in Java: A Beginner’s Guide in 5 Easy Steps
Fill in the details to know more
What Is the Use of Wrapper Class in Java?
March 22, 2023
What Is Clean Coding in Java?
March 21, 2023
What Are the New Features of Java 17?
What Is File Handling in Java?
March 16, 2023
What Is Data and Time Function in Java?
March 11, 2023
Top 10 Emerging Technologies Blogs To Read In 2023
December 15, 2022
History of Java Programming Language| UNext
November 18, 2022
An Advanced Guide to Shell Scripting in Unix!
November 15, 2022
Java Tutorial For Beginners
September 28, 2022
What Is Data Abstraction?
August 24, 2022
Encapsulation In Oops With Example
August 23, 2022
Add your details:
By proceeding, you agree to our privacy policy and also agree to receive information from UNext through WhatsApp & other means of communication.
Upgrade your inbox with our curated newletters once every month. We appreciate your support and will make sure to keep your subscription worthwhile