Access Modifiers in Java

Access modifiers determine the visibility of classes, constructors, methods, and variables within Java programs. They are used to restrict the scope or accessibility of classes, constructors, methods, and variables in a Java program.

There are four main access modifiers in Java:

  1. default: No access modifier is specified, to use default access modifier. When you declare a class, method, constructor, or variable without explicitly specifying an access modifier, it defaults to package-private. This means that they are accessible only within the same package. In other words, classes, methods, constructors, and variables with package-private access can be used and accessed by other classes that belong to the same package. However, they are not accessible outside of that package. Here's an example:
  2. // Employee.java
    package com.example.company;
    
    public class Employee {
        String name;
        int employeeId;
        // Other member variables and methods...
    
        // Constructor
        public Employee(String name, int employeeId) {
            this.name = name;
            this.employeeId = employeeId;
        }
    }
    
    // Department.java
    package com.example.company;
    
    public class Department {
        // Other department details...
    
        // Method accessing details of an employee within the same package
        void displayEmployeeDetails(Employee employee) {
            System.out.println("Employee ID: " + employee.employeeId);
            // Accessing other details within the same package
            System.out.println("Employee Name: " + employee.name);
        }
    }

    In this example, the Employee class has package-private access modifiers (default access) for its name and employeeId member variables. This means that these variables are accessible only within the same package (com.example.company). The Department class, which is also in the same package, can access these details directly.

    This usage of default access helps in organizing code within the same package while maintaining encapsulation. It ensures that the details of the Employee class are not exposed to classes outside the package, while still allowing necessary access within the package.

  3. private: The private access modifier is used to restrict access to a classes, constructors, methods, and variables from outside the class in which they are declared. Such private classes, constructors, methods, and variables are accessible only within the same class where they are declared and are not accessible in its subclasses and from any external class. Here's an example:
  4. // Employee.java
    package com.example.company;
    
    public class Employee {
        private String name;       // Private access modifier
        private int employeeId;    // Private access modifier
        // Other member variables and methods...
    
        // Constructor
        public Employee(String name, int employeeId) {
            this.name = name;
            this.employeeId = employeeId;
        }
    
        // Getter method for name (since it's private)
        public String getName() {
            return name;
        }
    
        // Getter method for employeeId (since it's private)
        public int getEmployeeId() {
            return employeeId;
        }
    }
    
    // Department.java
    package com.example.company;
    
    public class Department {
        // Other department details...
    
        // Method accessing details of an employee within the same package
        void displayEmployeeDetails(Employee employee) {
            System.out.println("Employee ID: " + employee.getEmployeeId());
            // Accessing other details using public getters within the same package
            System.out.println("Employee Name: " + employee.getName());
        }
    }

    In this example, the name and employeeId member variables in the Employee class are now declared as private. This means they can only be accessed within the same class (Employee), and not directly from other classes, even if they are in the same package.

    To allow controlled access to these private variables, public getter methods (getName and getEmployeeId) are provided. The Department class accesses the employee details using these public getters, demonstrating how encapsulation with private access modifiers can be used to control access to internal details of a class.

  5. protected: Classes, constructors, methods, and variables marked as protected are accessible within the same package. Additionally, subclasses can access protected methods and member variables of the superclass even if the subclass is located in different packages from the superclass. Here's an example:
  6. // Employee.java
    package com.example.company;
    
    public class Employee {
        protected String name;       // Protected access modifier
        protected int employeeId;    // Protected access modifier
        // Other member variables and methods...
    
        // Constructor
        public Employee(String name, int employeeId) {
            this.name = name;
            this.employeeId = employeeId;
        }
    
        // Getter method for name (since it's protected)
        protected String getName() {
            return name;
        }
    
        // Getter method for employeeId (since it's protected)
        protected int getEmployeeId() {
            return employeeId;
        }
    }
    
    // Department.java
    package com.example.company;
    
    public class Department {
        // Other department details...
    
        // Method accessing details of an employee within the same package
        void displayEmployeeDetails(Employee employee) {
            System.out.println("Employee ID: " + employee.getEmployeeId());
            // Accessing other details using protected getters within the same package
            System.out.println("Employee Name: " + employee.getName());
        }
    }

    In this example, the name and employeeId member variables in the Employee class are now declared as protected. This means they can be accessed within the same class (Employee) and by subclasses (including classes in the same package).

    The Department class, being in the same package, can access the employee details directly using the protected variables and methods. This demonstrates how the protected access modifier allows for more visibility than private but still enforces certain restrictions to maintain encapsulation and controlled access.

  7. public: Classes, constructors, methods, and variables marked as public are accessible from any other class, regardless of the location of the accessing code. Here's an example:
  8. // Employee.java
    package com.example.company;
    
    public class Employee {
        public String name;       // Public access modifier
        public int employeeId;    // Public access modifier
        // Other member variables and methods...
    
        // Constructor
        public Employee(String name, int employeeId) {
            this.name = name;
            this.employeeId = employeeId;
        }
    }
    
    // Department.java
    package com.example.company;
    
    public class Department {
        // Other department details...
    
        // Method accessing details of an employee within the same package
        void displayEmployeeDetails(Employee employee) {
            System.out.println("Employee ID: " + employee.employeeId);  // Accessing directly since it's public
            System.out.println("Employee Name: " + employee.name);      // Accessing directly since it's public
        }
    }

    In this example, both name and employeeId member variables in the Employee class are declared as public. This means they can be accessed directly from any other class, regardless of the package.

    The Department class, being in the same package, can access the employee details directly without the need for getter methods. This demonstrates how public access allows unrestricted access to the member variables, providing maximum visibility. However, it's important to note that using public variables directly exposes the internal details of the class, which may not be desirable in certain scenarios.