All topics
library
beginner

Access Modifiers

Understand the four access levels in Java and their scope: private, default, protected, public.

Four levels, and the one with no keyword is the one people forget.

Private = your diary (only you read it). Default = office memo (everyone in the department sees it). Protected = family secrets (relatives know, even in other cities). Public = newspaper (everyone reads it).

Key Concepts

1
    modifier      same class  same package  subclass  everywhere
    -------------------------------------------------------------
    private           yes          no          no         no
    (none)            yes          yes         no*        no
    protected         yes          yes         yes        no
    public            yes          yes         yes        yes
2
    * a subclass in the SAME package does see it
3
private is the default choice for fields. A subclass cannot see it, though it still exists in memory on every instance.
private
4
Package-private — no keyword at all — is visible across the package and nowhere else. It is the right level for a class that exists to support others in the same package, and it keeps that class out of your published API.
5
protected adds subclasses in any package. The catch is that subclass access works through this, not through someone else's reference:
protectedthis
6
    class Child extends Parent {
      void f(Parent other) {
        this.prot();        // fine
        other.prot();       // will not compile, if Parent is in another package
      }
    }
7
public is the API you must support forever. Everything else you can change.
public
8
An override cannot narrow access.
    class A { public void f() { } }
    class B extends A { protected void f() { } }    // will not compile
9
Widening is fine, because anything that worked against the parent must keep working against the child.
10
Top-level classes take only public or package-private. Nested classes may use all four.
public
11
Can a private method be overridden? No. The subclass cannot see it, so a matching signature there is simply a new method, and the parent keeps calling its own.
12
Reflection can bypass all of this with setAccessible(true), which is how serialization libraries populate private fields. The module system can refuse it.
setAccessible(true)
13
What the interviewer is probing.1. "Can a private method be overridden?" Probing: visibility versus inheritance. Stalls: "Yes, if the signature matches." Moves up: no — the subclass cannot see it, so a matching signature is a brand new method and the parent keeps calling its own.
14
2. "What is the default access level, and when would you use it?" Probing: whether you know package-private exists. Stalls: "It is public." Moves up: package-private — visible within the package only, and the right level for classes that support others in the package without joining the public API.
15
3. "Can an overriding method reduce visibility?" Probing: the substitution rule. Stalls: "Yes, to tighten it up." Moves up: no — anything that worked through the parent must keep working, so an override may widen access but never narrow it.
16
4. "protected means visible to subclasses. Can a subclass access it on another instance?" Probing: the detail inside protected. Stalls: "Yes, it is a subclass." Moves up: only through its own type — a subclass in a different package can access it via this, not via an arbitrary parent reference.