foreach-ui logo
codeLanguages
account_treeDSA

Quick Actions

quizlock Random Quiz
trending_uplock Progress
  • 1
  • 2
  • 3
  • 4
  • quiz
Java
  • Understand access modifiers and their importance
  • Learn the four modifiers: public, private, protected, default
  • Understand scope and visibility rules

Introduction to Access Modifiers in Java

Access modifiers are keywords that determine who can see and use your classes, methods, and variables. They're fundamental to encapsulation—one of OOP's core principles.

What are Access Modifiers?

Access modifiers control visibility of class members (fields, methods, constructors) from other classes. Java gives you four options:

1. Public (public)

The public modifier makes a class member accessible from anywhere in the program, including from different packages.

public class Car {
    public String model;  // Accessible from anywhere
    public void start() { // Accessible from anywhere
        System.out.println("Car started");
    }
}

2. Private (private)

The private modifier restricts access to within the same class only. Private members cannot be accessed from outside the class.

public class BankAccount {
    private double balance;  // Only accessible within this class
    
    private void updateBalance(double amount) {
        this.balance += amount;
    }
}

3. Protected (protected)

The protected modifier allows access within the same package and by subclasses (even in different packages).

public class Vehicle {
    protected String brand;  // Accessible in same package and subclasses
    
    protected void service() {
        System.out.println("Vehicle serviced");
    }
}

4. Default (Package-Private)

When no access modifier is specified, it has package-private access. Members are accessible only within the same package.

public class Calculator {
    int result;  // Package-private access
    
    void reset() {  // Package-private access
        result = 0;
    }
}

Access Modifier Scope

Modifier Same Class Same Package Subclass (Different Package) Everywhere
public ✅ ✅ ✅ ✅
protected ✅ ✅ ✅ ❌
default ✅ ✅ ❌ ❌
private ✅ ❌ ❌ ❌

Class-Level Access Modifiers

Classes can only be public or package-private (default). Inner classes can use all four modifiers.

public class PublicClass { }        // Accessible everywhere
class DefaultClass { }              // Package-private

public class OuterClass {
    public class PublicInner { }
    private class PrivateInner { }
    protected class ProtectedInner { }
    class DefaultInner { }
}

Why Use Access Modifiers?

  1. Encapsulation: Hide internal implementation details
  2. Security: Prevent unauthorized access to sensitive data
  3. Maintainability: Changes to private members don't affect other classes
  4. API Design: Control what clients can access

Common Mistakes

  1. Making all members public (breaks encapsulation)
  2. Using protected when private would suffice
  3. Forgetting that default access doesn't work across packages
  4. Making constructors private without providing factory methods

Access modifiers let you control visibility and implement proper encapsulation. The key is choosing the most restrictive modifier that still lets your code work—start with private and only open up as needed.

© 2026 forEach. All rights reserved.

Privacy Policy•Terms of Service