Quick revision
The short answer
private allows access only inside the top-level class, including its nested classes; package-private, the level we get by writing no modifier, allows any class in the same package; protected adds subclasses in other packages; public allows everyone, subject to module exports since Java 9. The extra that protected gives is access from a subclass in another package, but only through references of the subclass's own type or its subtypes, not through any instance of the parent type. Top-level classes can only be public or package-private.
What to remember
In this answer, we saw that private limits access to the top-level class and its nested classes, package-private to the same package, protected to the package plus subclasses elsewhere, and public to everyone, limited by module exports since Java 9. The precise extra that protected gives is access from a subclass in another package through references of the subclass's own type or its subtypes, not through arbitrary parent instances. Top-level classes can only be public or package-private, and in practice we pick the most restrictive level that works.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
private allows access only inside the top-level class, including its nested classes; package-private, the level we get by writing no modifier, allows any class in the same package; protected adds subclasses in other packages; public allows everyone, subject to module exports since Java 9. The extra that protected gives is access from a subclass in another package, but only through references of the subclass's own type or its subtypes, not through any instance of the parent type. Top-level classes can only be public or package-private.
Why this question is asked
Access modifiers look like beginner material, but the protected rule trips up experienced developers, usually when a subclass in another package fails to compile on a line that seems obviously allowed. Interviewers ask this to check that we know the exact rules rather than the approximate ones, and because visibility is the first tool we have for keeping a design's internals private. In this answer, we will go through the four levels from the most to the least restrictive, look closely at what protected allows and does not allow, see how Java 9 modules change public, and finish with the rules for top-level classes and how to choose a level.
1The Four Levels at a Glance
Java has four access levels, and three keywords for them. The fourth level, package-private, is what we get when we write no modifier at all. Each level allows everything the more restrictive one allows, plus a little more.
| Modifier | Same class | Same package | Subclass, other package | Anywhere |
|---|---|---|---|---|
| private | yes (incl. nested) | no | no | no |
| (none) package-private | yes | yes | no | no |
| protected | yes | yes | yes, via own type only | no |
| public | yes | yes | yes | yes, if package exported |
Two details are worth stating early. private is private to the top-level class, not to the nested class that declares the member, so an outer class and its nested classes can use each other's private members. And protected is wider than package-private, not narrower: it includes the whole package, which surprises developers coming from C++ or C#.
2Private and Package-Private
A private member is visible only to code inside the same top-level class. That includes nested classes, which is why a builder nested in a class can call the class's private constructor.
A package-private member, with no modifier, is visible to every class in the same package and to nothing outside it, not even subclasses in other packages. It is useful for code that collaborates within one package, such as a service and its helper classes, and for tests, which usually live in the same package as the code they test, in a different source folder.
3What protected Really Allows
protected adds one thing to package-private: a subclass in a different package can access the member. But there is a condition that most people do not know. Inside the subclass, the access must be through a reference whose type is the subclass itself or one of its subtypes. The subclass can use the protected member on itself and on other objects of its own kind, but not on an arbitrary object of the parent type.
The reason is the purpose of protected. It lets a subclass take part in implementing its parent's behaviour for its own objects. It is not meant to let any subclass reach into every Order in the system. Without the rule, anyone could write a tiny subclass of Order just to read or change the protected state of Order objects created by someone else.
The same idea applies to protected constructors. A subclass in another package can call one through super(...), but cannot write new Order() with it; and protected static members can be reached from any subclass, since there is no object involved.
4Public and Java 9 Modules
Before Java 9, public meant visible to every class on the classpath. With the module system, public means visible to everyone who can read the module, but only for packages that the module exports in its module-info.java. A public class in a package that is not exported is inaccessible from other modules on the module path, and even reflection with setAccessible fails unless the package is opened.
On the classpath, which is where most Spring Boot applications still run, the module rules do not apply, and public means public everywhere. So modules strengthen public only for code that actually runs on the module path.
5Top-Level Classes and Choosing a Level
A top-level class can only be public or package-private. private and protected do not make sense for it, because they are defined relative to an enclosing class, and a top-level class has none. Nested classes, being members, can use all four levels. A source file can contain at most one public top-level class, and it must match the file name.
The guidance for choosing is to give each member the most restrictive level that works. Fields are private. Classes and methods that only collaborate inside a package stay package-private, which is a good default for implementation classes. protected is a deliberate promise to subclass authors, with the same compatibility weight as public, so we use it only for real extension points. public is for the API we are willing to support.
6Follow-up questions
Can a subclass override a method and reduce its visibility?
No. An override must be at least as visible as the method it overrides, so a public method cannot be overridden as protected, and a protected one can be overridden as protected or public. Otherwise, code that works with the parent type could not call the method on a subclass instance.
Why can a test in the same package see package-private members, even from a different folder?
Packages are defined by the package name, not the directory. src/main/java and src/test/java both contribute classes to com.shop.order, and on the classpath they form one package. On the module path the test and main code must be in the same module for this to work, which build tools arrange by patching the module.
Does protected give access to a nested class in the same package?
Yes. Any code in the same package can access protected members, whatever the class relationship, because protected includes package access. The special subclass rule only matters for subclasses in other packages.
7Conclusion
In this answer, we saw that private limits access to the top-level class and its nested classes, package-private to the same package, protected to the package plus subclasses elsewhere, and public to everyone, limited by module exports since Java 9. The precise extra that protected gives is access from a subclass in another package through references of the subclass's own type or its subtypes, not through arbitrary parent instances. Top-level classes can only be public or package-private, and in practice we pick the most restrictive level that works.
Open with
There are four levels. private means only inside the top-level class, which includes its nested classes. Package-private, which is what I get with no modifier, means any class in the same package. protected is package access plus subclasses in other packages, and public is everywhere, although since Java 9, on the module path, public types are only reachable from other modules if their package is exported.
Then, if they want more
The subtle part is what protected adds. A subclass in a different package can use a protected member, but only through references of its own type or a subtype. So in RushOrder extends Order, I can write status on this or on another RushOrder, but plain.status on a variable of type Order fails to compile.
The reason is that protected exists so a subclass can help implement the parent's behaviour for its own objects, not so that anyone can write a subclass to reach into every Order in the system. Protected constructors work the same way: a subclass can call super, but cannot write new Order from another package.
Top-level classes can only be public or package-private, because private and protected only make sense relative to an enclosing class. My rule is the most restrictive level that works: private fields, package-private implementation classes, protected only for real extension points, and public for supported API.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain public, protected, package-private and private. What exactly does protected...": https://scrivio-interview-library.netlify.app/java/classes-and-objects/9f29150732/