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:
| Specifier | Accessible from |
|---|---|
public | any class, anywhere in the program |
protected | the same class, other classes in the same package, and subclasses (children) |
| default (no keyword written) | only other classes in the same package |
private | only inside the same class |
publicis the least restrictive — apublicmethod can be called from anywhere.privateis the most restrictive — aprivatefield is invisible outside its own class, which is exactly what we want for hidden data.protectedsits in between and is important for inheritance: a subclass can use aprotectedmember 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.