Skip to content
Exercises · Q1

Q.What are access specifiers in Java? Name the four levels and state, for each, from where a member can be accessed.

ChseodishaTextbookSubjectiveImportance★★★★★est
7% · 1/14 Questions
✓ Free question

Meaning. An access specifier (or access modifier) is a keyword written before a field, method or class that controls which parts of the program are allowed to access it. Java provides four levels, from most open to most restricted.

The four levels:

SpecifierAccessible from
publicany class, anywhere in the program
protectedthe same class, other classes in the same package, and subclasses (children)
default (no keyword written)only other classes in the same package
privateonly inside the same class
  • public is the least restrictive — a public method can be called from anywhere.
  • private is the most restrictive — a private field is invisible outside its own class, which is exactly what we want for hidden data.
  • protected sits in between and is important for inheritance: a subclass can use a protected member of its parent.
  • default (also called package-private) is what you get when you write no specifier at all.

Why they matter. By choosing the right specifier we expose only what the outside world should use (usually public methods) and hide the internal data (usually private fields) — the basis of encapsulation.

✓Final answer

The four access specifiers are public (anywhere), protected (class + package + subclasses), default (same package only), and private (same class only).

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.