Different Access Specifiers or Access Modifiers in Java

The access to classes, constructors, methods, and fields are regulated using access modifiers, i.e. a class can control what information or data can be accessible by other classes. To take advantage of encapsulation, you should minimize access whenever possible.

Different Access Specifiers or Access Modifiers in Java

Java provides several access modifiers to help you set the level of access you want for classes as well as the fields, methods, and constructors in your classes. A member has the package or default accessibility when no accessibility modifier is specified.

  1. Types of Java Access modifiers/specifiers
  2. Program examples, output and explanation.

Java Access Modifiers/Specifiers

There are four types–Public, Default, Protected and Private.

1] Public Access Specifiers

Fields, methods and constructors declared public (least restrictive) within a public class are visible to any class in the Java program, whether these classes are in the same package (collection of classes, similar to header files will see later) or in another package.

2] Default Access Specifiers

Java provides a default specifier which is used when no access modifier is present. Any class, field, method or constructor that has no declared access modifier is accessible only by classes in the same package. The default modifier is not used for fields and methods within an interface.

3] Protected Access Specifiers

The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods and constructors are protected in a superclass. They can be accessed only by subclasses in other packages. Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.

4] Private Access Specifiers

The private (most restrictive) fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface. Fields, methods or constructors declared private are strictly controlled, which means they cannot be accessed by anywhere outside the enclosing class. A standard design strategy is to make all fields private and provide public getter methods for them.

Below are the programs to demonstrate the use of public, private, protected and default access modifiers while accessing fields and methods. The outputs of each of these java files depict the Java, access specifiers.

Program Samples for Java Access Modifiers/Specifiers

Program 1

class Employee{
  public String employeeName;
  String address;
  protected int age;
  private double salary;

  void setSalary(double sal){
    salary = sal;
  }
  private double getSalary(){
    return salary;
  }
  void showDetails(){
    System.out.println("Employee's Name: "+employeeName);
    System.out.println("Employee's Address: "+address);
    System.out.println("Employee's Age: "+age);
  }
}

class AccessSpecifierDemo{
  public static void main(String args[]){
    Employee employee = new Employee();
    employee.employeeName = "Ronaldo";
    employee.address = "Portugal";
    employee.age = 29;
    employee.salary = 65034.00;
  }
}

Program 2

class Employee{
  public String employeeName;
  String address;
  protected int age;
  private double salary;

  void setSalary(double sal){
    salary = sal;
  }
  private double getSalary(){
    return salary;
  }
  void showDetails(){
    System.out.println("Employee's Name: "+employeeName);
    System.out.println("Employee's Address: "+address);
    System.out.println("Employee's Age: "+age);
  }
}

class AccessSpecifierDemo1{
  public static void main(String args[]){
    Employee employee = new Employee();
    employee.employeeName = "Ronaldo";
    employee.address = "Portugal";
    employee.age = 29;
    //employee.salary = 65034.00;
    employee.setSalary(65304.00);
    System.out.println("Employee's salary: "+employee.getSalary());
  }
}

Program 3

class Employee{
  public String employeeName;
  String address;
  protected int age;
  private double salary;

  void setSalary(double sal){
    salary = sal;
  }
  private double getSalary(){
    return salary;
  }
  void showDetails(){
    System.out.println("Employee's Name: "+employeeName);
    System.out.println("Employee's Address: "+address);
    System.out.println("Employee's Age: "+age);
    System.out.println("Employee's Salary: "+salary); //1
    System.out.println("Employee's salary: "+getSalary()); //2
  }
}

class AccessSpecifierDemo2{
  public static void main(String args[]){
    Employee employee = new Employee();
    employee.employeeName = "Ronaldo";
    employee.address = "Portugal";
    employee.age = 29;
    //employee.salary = 65034.00;
    employee.setSalary(65304.00);
    //System.out.println("Employee's salary: "+employee.getSalary());
    employee.showDetails();
  }
}

Output

code for access_specifier in java

Explanation of the Java Code & Output

Please carefully observe the code and the corresponding output of the same program. Now we will explain the program and their output. In all the programs we have declared one instance variable of each of the access specifier type.

Now if you see the first program, we have tried to initialize the private member salary of the employee class, it throws a compilation error saying pay has private access in Employee. That does mean private fields/variables cannot be accessed from outside of the class in which it is declared.

Now if you see the second program, we have defined a public method setSalary() to initialize private member variable salary. Here we will come across one more concept that private members will be available only in the class where it exists. But here we have defined a private method getSalary () to get the value of the salary. But also private methods of a class cannot be accessed from the outside of the class where it is defined. And we get the same compilation error getSalary() has private access in Employee. Hence, We are unable to get the value of salary printed outside of that class even though set by setSalary() method.

At last, if you see the third program, we get the value of salary and all the values of other instance variables printed in the default method showDetails() which is defined in the Employee class.

We will see one more example on access specifier after completing discussion on inheritance and package.

About the author

Nitin Agarwal

A blogger, tech evangelist, YouTube creator, books lover, traveler, thinker, and believer of minimalist lifestyle.

3 Comments

Click here to post a comment