What are Access Modifiers in Java?

In this tutorial, we will discuss what are access modifiers in Java, their uses, when to use them and how to implement them in a Java program. We will explore different access modifiers that are available in Java programming.

What are Access Modifiers in Java?

Access Modifiers are used to scope the variables, methods, and other members of a class. It is used to restrict the access of different elements of a class.

what are access modifiers in java

These specifiers are actually keywords in Java. There are 4 types of access specifiers in Java, they are:

  1. public
  2. private
  3. protected
  4. (default)

Note: To better understand access specifiers, we need to have an understanding of packages.
Let’s discuss access specifiers one by one.

public

The keyword used for the public access specifier is ‘public’.
Any variable, class, method, or any other member of a class that is declared as public will be accessible from anywhere in the program. They will have the maximum scope that is possible in a Java program.

Let’s understand the public access specifier with an example.
Consider a package pkg1 which contains the class below.

package pkg1;
public class Calculate {
    public int additionOfNums(int firstNumber, int secondNumber) {
        return firstNumber+secondNumber;
    }
}

Consider a package pkg2 which contains the class below. It imports the class Calculate from package pkg1.

import pkg1.Calculate;
public class Program{
    public static void main(String agmts[]) {
        int A = 10;
        int B = 15;
        Calculate cal = new Calculate();
        int aplusb= cal.additionOfNums(A,B);

        //Printing A + B
        System.out.println("We get: " + aplusb);
    }
}

/* Output
We get: 25
*/

In the above example, the package pkg1 contains the class Calculate which has the public access specifier. Due to this specifier, it is being accessed and used in other packages (pkg2).

private

The keyword used for the private access specifier is ‘private’.
Any variable, class, method, or any other member of a class that is declared as private will be accessible only within the class it is a member of. Only the members of that class where it belongs will be able to access it. The private access specifier is the most restrictive access specifier among all other specifiers.

Let’s understand the private access specifier with an example.
Consider a package pkg1 which contains the class below.

package pkg1;
public class Calculate {
    private int sum(int a, int b) {
        return a+b;
    }
    public int additionOfNums(int firstNumber, int secondNumber) {
        return sum(firstNumber,secondNumber);
    }
}

Consider a package pkg2 which contains the class below. It imports the class Calculate from package pkg1.

import pkg1.Calculate;
public class Program{
    public static void main(String agmts[]) {
        int A = 76;
        int B = 14;
        // This will generate an error
        // int sum = cal.sum(A,B);

        // This is work
        Calculate cal = new Calculate();
        int sum = cal.additionOfNums(A,B);

        //Printing A + B
        System.out.println("We get: " + sum);
    }
}

/* Output
We get: 90
*/

In the above example, we have 2 methods in package pkg1. One of them is a private method and another one a public. We are calling the additionOfNums() method from pkg2. This method is in another package and its access specifier is public. So we can access it directly. The additionOfNums() method then calls the sum() method which it has access to because private members can be accessed within a class.
In case we called sum() method directly from package pkg2 would have caused an error in the program.

protected

The keyword used for the protected access specifier is ‘protected’.
Any variable, class, method, or any other member of a class that is declared as protected will be accessible within the same package it is in as well as in other packages where it is being inherited. Let’s understand the protected access specifier with an example.
Consider a package pkg1 which contains the class below.

package pkg1;
public class Calculate {
    protected int additionOfNums(int firstNumber, int secondNumber) {
        return firstNumber+secondNumber;
    }
}

Consider a package pkg2 which contains the class below. It imports the class Calculate from package pkg1.

import pkg1.Calculate;
public class Program extends Calculate{
    public static void main(String agmts[]) {
    int A = 64;
    int B = 13;

    // The add () method is not accessible directly be instantiating the class
    Calculate cal = new Calculate();
    //int sum = cal.additionOfNums(A,B);

    // The Calculate class is inherited, so we can access it by creating object in main()
    Program prog = new Program();
    int sum = prog.additionOfNums(A,B);

    //Printing A + B
    System.out.println("We get: " + sum);
    }
}

/* Output
We get: 77
*/

default

The default access specifier is not actually a keyword. When there is no access specifier specified for a class or members of a class, then it is by default assumed as the default access specifier. It doesn’t require any keyword.

The scope of the default access specifier is everywhere within the same package and not outside the package. Let’s understand with an example.
Consider a package pkg1 which contains the class below.

package pkg1;

class Multiply {
    int multiply(int a, int b) {
        return a*b;
    }
}

class Calculate extends Multiply{
    // This class can access/inherit Multiply class because they are in the same package
}

Consider a package pkg2 which contains the class below. It imports the class Calculate from package pkg1.

import pkg1.Calculate; // Cannot be imported
class Program extends Calculate{
    public static void main(String agmts[]) {
        // This program will generate an error because the Calculate class in package pkg1 has default access
        // Due to which it is only used/accessed in package pkg1
    }
}

/* Output
Sum of A and B: 77
*/