Java Access Modifiers: Public, Private, Protected

Access modifiers in Java determine the scope and accessibility of classes, interfaces, variables, and methods. They help to ensure that data and methods are accessible only to the intended parts of the code. Java has four types of access modifiers: public, private, protected, and the default (no modifier). Let’s discuss each one with examples.

Public Access Modifier

The public access modifier allows the widest accessibility. A public class, method, or field can be accessed from any other class in the project.

Use Case: This modifier is typically used for methods and variables that need to be accessed from multiple parts of a project, including from classes in different packages.

Example:

public class MyClass {
    public int value = 100;

    public void printValue() {
        System.out.println("Value: " + value);
    }
}

// Another class accessing the public member
class Test {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.printValue(); // Outputs "Value: 100"
    }
}

Private Access Modifier

The private access modifier is the most restrictive. It makes a method, variable, or inner class accessible only within the class where it’s defined.

Use Case: Use private when you want to hide the implementation details of a class and prevent external classes from accessing or modifying those details. This contributes to the principle of encapsulation.

Example:

class MyClass {
    private int value = 100;

    private void printValue() {
        System.out.println("Value: " + value);
    }

    public void accessPrivateMethod() {
        printValue(); // Outputs "Value: 100"
    }
}

Protected Access Modifier

The protected access modifier provides more accessibility than private, but less than public. It allows access within the same package and also by subclasses outside the package.

Use Case: Protected is often used in a parent class to ensure that only its subclasses, regardless of their package, can access or override certain methods.

Example:

package package1;

public class Parent {
    protected int value = 100;

    protected void printValue() {
        System.out.println("Value: " + value);
    }
}

package package2;
import package1.Parent;

class Child extends Parent {
    public void display() {
        printValue(); // Outputs "Value: 100"
    }
}

Default Access Modifier (No Modifier)

When no access modifier is specified, it’s known as the default access level. This allows a member to be accessed within the same package but not from outside the package.

Use Case: Default access is useful when you want to restrict access to the members within the same package, promoting modular development.

Example:

package package1;

class MyClass {
    int value = 100;

    void printValue() {
        System.out.println("Value: " + value);
    }
}

// Another class in the same package
class Test {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        myClass.printValue(); // Outputs "Value: 100"
    }
}

Comparing Access Modifiers

Here’s a quick comparison of all four access modifiers:

Modifier Same Class Same Package Subclass Any Class
Public Yes Yes Yes Yes
Private Yes No No No
Protected Yes Yes Yes No
Default Yes Yes No No

Conclusion

Java’s access modifiers offer varying degrees of visibility, from the most restrictive private to the most accessible public, with protected and the default level in between.

  • Public: Use when you want a member to be accessible from any location.
  • Private: Use when a member should only be accessible within its own class.
  • Protected: Useful when a member should be accessible within the same package and to subclasses.
  • Default: Ideal for restricting access to within the same package.