Access modifiers (also known as access specifiers) determine the scope and visibility of classes, constructors, fields, and methods across package and subclass boundaries in Java.
private: Accessible strictly within the declaring class only. Invisible to all other classes, even within the same package.default (no keyword): Accessible to any class within the same package (package-private). Invisible outside the package.protected: Accessible within the same package, and accessible to subclasses in external packages via inheritance.public: Accessible from anywhere in the entire application or external modules.| Location of Access | private |
default (none) |
protected |
public |
|---|---|---|---|---|
| Same Class | YES | YES | YES | YES |
| Same Package (Non-subclass) | NO | YES | YES | YES |
| Different Package (Subclass via inheritance) | NO | NO | YES | YES |
| Different Package (World / Non-subclass) | NO | NO | NO | YES |
Exam Question: "Can an overridden method reduce the visibility of the superclass method?"
Answer: No! An overriding method can maintain the same access or make it more accessible, but never more restrictive. For example, if a parent method is protected, the subclass can declare it protected or public. If you attempt to make it private or default, the compiler halts with: attempting to assign weaker access privileges.
Now let us explore Abstraction and Abstract Classes. Proceed to: Java Abstraction & Abstract Classes →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Data hiding using private.
Package boundaries and imports.
Protected inheritance.
Abstract classes visibility.