Access Modifiers in Java: An Easy 3 Step Beginner’s Guide

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:

  1. What Are Access Modifiers In Java?
  2. What Is The Use Of Access Modifiers In Java?
  3. What Are The Types Of Access Modifiers In Java?

1. What Are Access Modifiers In Java?

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.

2. What Is The Use Of Access Modifiers In Java?

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.

3. What Are The Types Of Access Modifiers In Java?

The four different Access Modifiers in Java are:

  • Default – They are also called package-private Access Modifiers as the declarations made using Default Modifiers are visible only within the package.
  • Private – The declarations made using Private Modifiers are visible within the class only.
  • Protected – The declarations made using Protected Modifiers are visible within the package or all subclasses.
  • Public – The declarations made using Public Access Modifiers are visible everywhere.

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.

Same classSame package subclassSame package non-classDifferent package subclassDifferent package non-subclass
DefaultYesYesYesNoNo
PrivateYesNoNoNoNo
ProtectedYesYesYesYesNo
PublicYesYesYesYesYes

For a better understanding, let’s have a look at the different types of Access Modifiers in Java with examples.

Default Access Modifiers

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.

Private Access Modifiers

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.

Protected Access Modifiers

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.”

Public Access Modifiers

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 class A

{

   public void display()

      {

          System.out.println(“Jigsaw Academy”);

      }

}

package s2;

import s1.*;

class B

{

    public static void main(String args[])

      {

          A obj = new A;

          obj.display();

      }

}

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.

Conclusion

Summing up the key points of Access Modifiers in Java:

  • Access Modifiers in Java are used to define the accessibility of methods and data members.
  • There are four main types of Access Modifiers – Default, Private, Protected, and Public.
  • Each modifier has varying levels of restrictions. The Public Modifier has the least restrictions, whereas the Private Access Modifier has the most limitations.
  • If there is no Access Modifier defined, it is treated as a Default Access Modifier.

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.

Also, Read

What is the Collection Framework in Java?

Multithreading in Java: A Beginner’s Guide in 5 Easy Steps

Related Articles

} }
Request Callback