News

Different Access Specifiers or Access Modifiers in Java

code for access_specifier 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.

Java provides a number of 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 package or default accessibility when no accessibility modifier is specified.

Java Access Modifiers/Specifiers

  1. Public
  2. Default
  3. Protected
  4. Private

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.

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.

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 declared protected in a superclass 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.

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 accesses 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 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 carefully 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 salary 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 can be accessed 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.

2 Comments

2 Comments

  1. Viruu Krish

    April 25, 2012 at 12:06 PM

    Too Good

  2. Mirzan Miftha

    January 20, 2012 at 3:38 PM

    Thanks A Lot for the post !
    info depicts a clear idea regarding access level

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top