← Back to Home

Access Modifiers

Access modifiers in Java control the visibility and accessibility of classes, variables, methods, and constructors. They are a core mechanism for encapsulation, security, and clean API design. This is a must-know interview topic and heavily used in real-world Java projects.

What Are Access Modifiers?

  • Keywords that define where a member can be accessed from
  • Enforce data hiding and controlled exposure
  • Applied to classes, methods, variables, and constructors

Types of Access Modifiers in Java

Java provides four access levels:

  1. private
  2. default (no keyword)
  3. protected
  4. public

Access Level Hierarchy (Least → Most Accessible)

private < default < protected < public
          

1. private

  • Accessible only within the same class
  • Most restrictive
  • Commonly used for instance variables
class Student {
    private int age;

    private void calculate() { }
}
          
  • ✔ Supports strong encapsulation
  • ❌ Not accessible outside the class

2. default (Package-Private)

  • No keyword used
  • Accessible within the same package only
  • Not accessible from outside the package
class Student {
    int marks; // default access
}
          
  • ✔ Useful for package-level collaboration
  • ❌ No access across packages

3. protected

  • Accessible within the same package
  • Accessible in subclasses, even in different packages
class Parent {
    protected int value;
}
          
  • ✔ Useful in inheritance
  • ✔ Common in framework design

4. public

  • Accessible from anywhere
  • Least restrictive
  • Used for APIs, entry points, and services
public class Student {
    public void display() { }
}
          
  • ✔ Required for main() method
  • ✔ Used for externally exposed functionality

Access Modifiers Visibility Table (Interview Favorite)

Access Modifier Same Class Same Package Subclass (diff pkg) Everywhere
private
default
protected
public

Access Modifiers with Class Declaration

Top-Level Classes

  • Only public and default are allowed
public class Test { }
class Helper { } // default
          

❌ private and protected are not allowed for top-level classes.

Access Modifiers with Constructors

class Example {
    private Example() { }   // restrict object creation
    public Example(int x) { }
}
          
  • ✔ Used in Singleton pattern
  • ✔ Controls object creation

Access Modifiers and Encapsulation (Best Practice)

class Account {
    private double balance;

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}
          
  • Data hidden
  • Controlled access
  • Business rules enforced

Common Beginner Mistakes

  • Making variables public
  • Confusing protected with default
  • Assuming protected means package-only
  • Overexposing internal methods
  • Not understanding package boundaries

Interview-Ready Answers

Short Answer

Access modifiers control the visibility of classes, variables, and methods in Java.

Detailed Answer

Java provides four access modifiers: private, default, protected, and public. They define where a class member can be accessed from and are essential for encapsulation, security, and maintainable object-oriented design.

Key Takeaway

Use the most restrictive access level possible. Good access control leads to secure, maintainable, and flexible Java applications.