Skip to main content Skip to docs navigation
TechSpiderTutorials

Java Access Specifiers - Public, Protected, Private, and Default

Introduction to Access Specifiers

Access specifiers in Java define the scope and visibility of classes, methods, and variables. They regulate how these components can be accessed from other classes and packages, ensuring proper encapsulation and control over data.

Types of Access Specifiers

public Access Specifiers

The public specifier makes a class, method, or variable accessible from any other class.

public class MyClass {
    public void publicMethod() {
        // Code accessible from anywhere
    }
}

protected Access Specifiers:

The protected specifier allows access within the same package and by subclasses (even if they are in different packages).

public class Parent {
    protected int protectedVariable = 10;
}

public class Child extends Parent {
    public void accessProtected() {
        System.out.println(protectedVariable); // Accessible in subclass
    }
}

private Access Specifiers:

The private specifier restricts access to only within the same class.

public class MyClass {
    private int privateVariable = 5;
    
    public void accessPrivate() {
        System.out.println(privateVariable); // Accessible within the same class
    }
}

Default (Package-Private) Access Specifiers:

If no access specifier is specified (also known as package-private), the class, method, or variable is accessible only within the same package.

class PackageClass {
    void packageMethod() {
        // Accessible within the same package
    }
}

Access Specifier Rules and Guidelines

  • Visibility Hierarchy: public > protected > default (package-private) > private
  • Inheritance: Subclasses inherit protected and public members; private members are not inherited.
  • Encapsulation: Use private for internal data hiding and limit direct access to variables.

Best Practices for Using Access Specifiers

  • Encapsulate Data: Use private for variables and provide public methods (getters and setters) to access or modify them.
  • Minimize Access: Restrict access as much as possible (private by default) and grant access only when necessary (protected or public).
  • Package Structure: Design packages to group related classes and define appropriate access levels (default for package-specific logic).

Examples of Access Specifiers in Action

Example 1: Using public Access Specifier
public class Example {
    public void publicMethod() {
        System.out.println("This method is accessible from anywhere.");
    }
}

Example 2: Using private Access Specifier for Data Hiding:

public class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        this.balance = initialBalance;
    }

    public void deposit(double amount) {
        balance += amount;
    }

    public void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
        } else {
            System.out.println("Insufficient funds.");
        }
    }

    public double getBalance() {
        return balance; // Accessible through a public method
    }
}