Comparing Access Levels: A Decision Matrix
You'll learn how to choose the right access level by comparing all four modifiers side-by-side.
Now that you've seen private, package-private (default), protected, and public individually, it's time to see them together. Choosing the correct access level is like choosing who gets keys to different parts of a building: some areas are open to everyone, others restricted to employees, and some locked to specific departments only.
The Access Level Matrix
Here's how the four levels compare in terms of visibility:
| Modifier | Same Class | Same Package | Subclass | Everywhere |
|---|---|---|---|---|
private | ✓ | ✗ | ✗ | ✗ |
| (default) | ✓ | ✓ | ✗ | ✗ |
protected | ✓ | ✓ | ✓ | ✗ |
public | ✓ | ✓ | ✓ | ✓ |
When to Use Each
private: Internal implementation details that no one outside the class should touch. Use for fields and helper methods.
Package-private (default): Utilities shared among related classes in the same package. Good for package-internal collaboration.
protected: Methods or fields that subclasses might need to override or access. Less common than private or public.
public: The official API of your class. Only expose what's necessary for outside code.
The Golden Rule
Start with private and only increase visibility when needed. It's much easier to make something more accessible later than to restrict access after code depends on it.
Key Takeaway: Use the most restrictive access level possible—private by default, then widen only when collaboration or inheritance demands it.