SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 3 / 42

Junior

What are Java's four access modifiers, and where exactly is protected accessible?

Short answer

From narrow to wide: private, default (same package), protected (adds subclasses in other packages), public. The trap: a subclass in another package uses protected members only through a reference of its own type.

its own type, thisparent typeSubclass in other packageuses protected memberThrough which referencetype?AllowedCompile error

3 points to say:

  1. private < default < protected < public.
  2. protected is also open to the same package.
  3. Fields are always private.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.