Java Access Modifiers

In Java, access modifiers are used to set the visibility and accessibility of classes, methods, constructors, and variables. They help control how the members of a class (fields and methods) are accessed from other classes or packages.

There are four main access modifiers in Java:

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

public Access Modifier

public members are accessible from anywhere in the project (from any class, package, or even outside the package).


class Employee {

    public String name;
    
    Employee(String name) {
        this.name = name;
    }
    
    public void getEmployeeName() {
        System.out.println("Name: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp1 = new Employee("John");
        // Accessing the public method getEmployeeName
        emp1.getEmployeeName();  // Output: Name: John
    }
}

Note: The public keyword allows the publicValue field and displayPublicValue method to be accessed from any other class, inside or outside the package.

private Access Modifier

private members are only accessible within the same class. They cannot be accessed from outside the class, not even by subclasses.


class Employee {

    public String name;
    private String organisation = "TCS"; // private variable organisation
    
    Employee(String name) {
        this.name = name;
    }
    public String getOrganisation() {
        return organisation;  // call private variable into public method
    }
    
    public void getEmployeeName() {
        System.out.println("Name: " + name);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee("John");
        
        System.out.println("Organisation: " + emp.getOrganisation());
        
        // Accessing the public method getEmployeeName
        emp1.getEmployeeName();  // Output: Name: John
    }
}

The field privateValue is not directly accessible from outside the class. To access or modify it, you must use the provided public getter and setter methods.

protected Access Modifier

protected members are accessible within the same package and by subclasses, even if the subclass is in a different package.


class Employee {

    public String name;
    protected String designation;
    
    public Employee(String name) {
        this.name = name;
        designation = "Manager"; // Default designation
    }
}

class Manager extends Employee {

    public Manager(String name) {
        super(name);  // Explicitly calling the Employee constructor
    }
    
    public void displayDesignation() {
         System.out.println("Designation: " + designation);  // Accessing the protected field
    }
}

public class Main {
    public static void main(String[] args) {

        // Creating a Manager object and displaying the name and designation
        Manager mg = new Manager("Tom");  
        System.out.println("Name: " + mg.name); 
        mg.displayDesignation();  // Output: Designation: Manager
    }
}

Output:

Name: Tom
Designation: Manager

Explanation:

1. Manager class inherits all non-private fields and methods of Employee, which includes the name and designation fields.

2. The constructor Manager(String name) explicitly calls the Employee constructor using super(name). This allows the Manager object to initialize its name attribute using the Employee constructor.

3. The displayDesignation() method prints out the designation. Since designation is a protected attribute in Employee, it is accessible within the Manager class (which is a subclass).

Default (Package-Private) Access Modifier

If no access modifier is specified (i.e., no public, private, or protected), the member is accessible only within the same package. It is often referred to as package-private access.

Example:


class Employee {

    String name;

    public Employee() {
        name = "John";
    }
    
    public void displayName() {
        System.out.println("Package-private Value: " + name);
    }
}


public class Main {
    public static void main(String[] args) {

        // Creating a Employee object and displaying the name
        Employee emp = new Employee();  
        System.out.println("Name: " + emp.name); // Accessible because it's in the same package
        emp.displayName();  // Output: Employee Name: John
    }
}

Note: If no access modifier is specified, the default is package-private. The class member is accessible only within the same package.