01 / 20

Java Access
Modifiers

Master the four levels of visibility control in Java: understand how public, private, protected, and package-private protect your code.

Java

What Are Access Modifiers?

Access modifiers are keywords in Java that control which parts of your code can see and use a class, method, or variable. They are the foundation of encapsulation: the principle of hiding internal details and exposing only what is necessary.

Why Do They Matter?

1
Data Protection: Prevent external code from accidentally corrupting your object's internal state.
2
Clean APIs: Expose only the methods users need, hiding complex implementation details.
3
Easier Maintenance: Change internal code freely without breaking code that depends on your class.
4
Team Collaboration: Clearly signal which parts of your code are safe for others to use.

The Four Levels of Access

public
Accessible from anywhere in any package, any class.
protected
Same package + subclasses in other packages.
package-private
Same package only. The default when no modifier is written.
private
Only the declaring class itself. Most restrictive.

Visibility Scope Diagram

public protected package-private (default) private MyClass Everywhere Package + Subclasses Same Package Same Class

The Core Rule

1
Fields should almost always be private. Expose them through getter/setter methods.
2
Helper methods used only inside a class should be private.
3
Methods subclasses need to override should be protected.
4
API methods that external code must call should be public.

protected vs. package-private

These two are the most commonly confused. The critical difference is what happens outside the package.

protected package-private
Same class ✓ ✓
Same package ✓ ✓
Subclass (different pkg) ✓ ✕
Non-subclass (different pkg) ✕ ✕

How to Choose the Right Modifier

Does external code need it? Yes public No Do subclasses need to override? Yes protected No Do same-package classes need it? Yes default No private

Example: private Fields

public class BankAccount { private double balance; public double getBalance() { return balance; } public void deposit(double amount) { if (amount > 0) { balance += amount; } } }

Example: protected in Inheritance

// File: animals/Animal.java public class Animal { protected String name; protected void makeSound() { System.out.println("..."); } } // File: pets/Dog.java (different package) public class Dog extends Animal { public void bark() { name = "Buddy"; // ✓ OK: subclass makeSound(); // ✓ OK: subclass } }

Example: Package-Private Default

// File: utils/StringHelper.java class StringHelper { // No modifier = package-private String capitalize(String s) { // Also package-private return s.substring(0,1).toUpperCase() + s.substring(1); } } // File: utils/UserFormatter.java (same package: OK) public class UserFormatter { StringHelper helper = new StringHelper(); // ✓ OK } // File: app/Main.java (different package: BLOCKED) // StringHelper helper = new StringHelper(); ✕ Error!

Modifiers on Classes

Top-level classes (not nested) can only be public or package-private. You cannot declare a top-level class as private or protected.

public class
File must be named ClassName.java. Only one public class per file.
default class
Visible only within its package. No filename restriction (can have many per file).

Worked Example: Designing a Class

public class Student { private String name; // Only this class private int grade; // Only this class protected String schoolId; // Subclasses too public String getName() { // Everyone return name; } private boolean isValid() { // Internal only return grade >= 0 && grade <= 100; } void printReport() { // Package-private if (isValid()) { /* ... */ } } }

Common Mistakes

✕ Making Everything Public
Exposing all fields as public destroys encapsulation. External code can corrupt your data without validation.
✕ Forgetting Default Exists
Omitting a modifier doesn't mean "public" or "private": it means package-private, which is its own distinct level.
✕ Confusing protected Scope
protected is not "same package only." It also grants access to subclasses in other packages.

Complete Comparison Table

Scope public protected default private
Same class ✓ ✓ ✓ ✓
Same package ✓ ✓ ✓ ✕
Subclass (other pkg) ✓ ✓ ✕ ✕
Non-subclass (other pkg) ✓ ✕ ✕ ✕

Question 1 of 5

A field declared with no access modifier in Java has what level of visibility?

Question 2 of 5

Which access modifier should you use for a field that stores sensitive data (like a password) in a class?

Question 3 of 5

A protected method in class Animal (package: creatures) can be accessed by which of the following?

Question 4 of 5

Which of the following is NOT a valid access modifier for a top-level class in Java?

Question 5 of 5

Which classes can access A.count?