Java OOP · Module 5
public, protected, package-private, private
Reading the visibility matrix without memorising it
What you'll be able to do
- List the four access levels from most open to most closed
- Decide, for a given piece of code, whether an access is legal
- Explain what a package is and what package-private means
- Choose the right modifier for fields, methods and classes
Access Modifiers
The four levels and the one matrix that decides everything
Java asks one question about every access: 'from where are you asking?' The four modifiers are just four different answers.
An access modifier is a keyword you put in front of a field, method, constructor or class to say who may use it. Before the matrix, one word you need: a package is a named folder of related classes (package com.bank.core; at the top of the file). Classes in the same package are considered 'neighbours' by the compiler.
The four levels, from most open to most closed:
The visibility matrix
| Access from | private | package-private | protected | public |
|---|---|---|---|---|
| Same class | ✅ | ✅ | ✅ | ✅ |
| Another class, same package | ❌ | ✅ | ✅ | ✅ |
| Subclass, same package | ❌ | ✅ | ✅ | ✅ |
| Subclass, different package | ❌ | ❌ | ✅ (through inheritance) | ✅ |
| Unrelated class, different package | ❌ | ❌ | ❌ | ✅ |
Read it as a staircase: each level down adds one more group of callers. The only row people get wrong is the fourth — see the callout below.
All four in one class
.java file, and it must match the file name.Employee can read or write it, not even a subclass.Manager may use it directly; unrelated classes in other packages may not.private + accessor.Run it. Then add `System.out.println(t.celsius);` in main and read the compiler error — that is the matrix being enforced.
The protected subtlety interviewers love
A subclass in another package may access a protected member through inheritance (this.department, or department), but *not* through a reference to some other Employee object (otherEmployee.department). Protected says 'you may use what you inherited', not 'you may inspect every instance'.
Prove it — coding challenges
Package-private Access
Choosing a modifier on purpose
The right question is never 'what works?' — everything works with public. It is 'what is the smallest audience that can do the job?'
A decision procedure you can apply in seconds
- Start every field at private. If nothing breaks, you are done.
- Needed by a *subclass*? Prefer a protected method over a protected field — behaviour can validate, a field cannot.
- Needed by a *neighbour class* in the same module? Leave it package-private and keep it out of your public API.
- Needed by *outside code*? Make it public — and accept that you now support it forever.
protected field vs protected method
protected double balance;
every subclass can corrupt it
- Subclasses bypass all validation
- You can never change the field's type or name safely
- Bugs appear in classes you do not own
private field + protected method
subclasses get behaviour, not raw state
- Validation still runs
- Internals stay changeable
- The parent keeps its invariants
Practical drills — is this access allowed?
| Situation | Allowed? | Reason |
|---|---|---|
Main (same package) reads a package-private field | ✅ | same package is enough |
Manager extends Employee (other package) reads this.department (protected) | ✅ | inherited member |
Manager (other package) reads other.department on another Employee | ❌ | not through inheritance |
A subclass reads the parent's private field | ❌ | private never crosses the class boundary |
| A class in another package calls a public method | ✅ | public means everyone |
| A class in another package touches a package-private method | ❌ | different package |
In this playground everything shares one file
The sandbox compiles your classes together as if they were in one package, so package-private and protected behave like 'visible'. private is still enforced, and the concept checker verifies your modifiers by reading the code — so write the modifier you would write in a real project.
Prove it — coding challenges
Common mistakes
Thinking `protected` is 'more private' than package-private.Making fields public 'just for now' to get the code compiling.Writing `private` on a class you want other files to use.Lesson playground
An Employee with all four levels, and a Manager subclass that uses what it is allowed to use. Try replacing `getSalary()` in describe() with `salary` and read the error.
Check understanding
Quiz
Question 1 of 5
Report is in package com.app.web. Invoice is in package com.app.core and has a protected String code;. Report does not extend Invoice. Can Report read code?
Summary
- Order of openness: private < package-private (no modifier) < protected < public.
- A package is a named group of classes; sub-packages are separate packages.
- `protected` = same package + subclasses, and only through inheritance for outside-package subclasses.
- Default to private, widen only for a concrete, named caller.