Java Access Modifiers

 

In Java, access modifiers define the visibility and accessibility of classes, methods, and variables. They form the foundation of encapsulation—one of the four pillars of Object-Oriented Programming (OOP).

  • By using access modifiers correctly, you can:
    Protect sensitive data
  • Control what parts of code can interact with others
  • Improve maintainability and security

In this guide, we’ll explore:

  • What access modifiers are
  • Types of access modifiers in Java
  • Practical examples
  • Best practices

What Are Access Modifiers?

 

Access modifiers are keywords in Java that set the visibility level for classes, attributes, methods, and constructors.
There are four access levels in Java:
1. public
2. private
3. protected
4. default (no modifier)

1. public

 
  • Accessible from anywhere (same class, same package, different package).
  • Used for APIs and widely available methods.

Example:

public class PublicExample {
  public String name = “Alice”;

  public void showName() {
    System.out.println(“Name: ” + name);
  }
}

2. private

 
  • Accessible only within the same class.
  • Not accessible from subclasses or other packages.
  • Used for encapsulating sensitive data.

Example:

public class PrivateExample {
  private int age = 25;

  private void showAge() {
    System.out.println(“Age: ” + age);
  }

  public void accessPrivate() {
    showAge(); // accessible within the same class
  }
}

3. protected

 
  • Accessible in:
    • Same class
    • Same package
    • Subclasses (even in different packages)
  • Commonly used for inheritance scenarios.

Example:

class Animal {
  protected String type = “Mammal”;

  protected void showType() {
    System.out.println(“Type: ” + type);
  }
}

class Dog extends Animal {
  public void display() {
    showType(); // accessible in subclass
  }
}

4. Default (Package-Private)

 
  • If no modifier is specified, the member is accessible only within the same package.
  • Not accessible from outside the package.

Example:

class DefaultExample {
  String city = “Hyderabad”; // package-private

  void showCity() {
    System.out.println(“City: ” + city);
  }
}

Access Modifier Visibility Table

 

Modifier

Same Class

Same Package

Subclass (diff pkg)

Other Packages

public

✅

✅

✅

✅

protected

✅

✅

✅

❌

default

✅

✅

❌

❌

private

✅

❌

❌

❌

Example: Combining All Access Modifiers

 

package com.example;

public class AccessDemo {
  public int pubVar = 1;
  protected int protVar = 2;
  int defaultVar = 3; // package-private
  private int privVar = 4;

  public void show() {
    System.out.println(“Public: ” + pubVar);
    System.out.println(“Protected: ” + protVar);
    System.out.println(“Default: ” + defaultVar);
    System.out.println(“Private: ” + privVar);
  }
}

Best Practices

 
  • Use private fields with getters/setters for encapsulation.
  • Use public only when absolutely necessary.
  • Use protected for inheritance-friendly designs.
  • Prefer default for package-level utilities.
  • Don’t overexpose class members—least privilege principle.

Conclusion

 

Access modifiers are a powerful tool in Java to control visibility and enforce encapsulation.

  • Use private for sensitive fields.
  • Use protected for inheritance.
  • Use default for package-level access.
  • Use public sparingly for APIs and widely used methods.

By mastering access modifiers, you’ll build secure, maintainable, and robust Java applications.

Scroll to Top