A protected method in Java is a class member defined with theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
protected access modifier, restricting its visibility to the declaring class, classes within the same package, and subclasses located in any package. It establishes an access level that is strictly wider than default (package-private) visibility but more restrictive than public visibility.
Visibility Rules
The Java compiler enforces the following access boundaries for aprotected method:
- Same Class: Accessible.
- Same Package (Subclass): Accessible.
- Same Package (Non-subclass): Accessible.
- Different Package (Subclass): Accessible (with strict instance restrictions).
- Different Package (Non-subclass): Not accessible.
Cross-Package Subclass Access Restriction
When a subclass resides in a different package from the superclass declaring theprotected method, the subclass can only invoke the method through inheritance on instances of its own type (or its own subclasses). It cannot invoke the protected method on a direct reference of the superclass type.
Method Overriding Constraints
According to the Java Language Specification (JLS), when a subclass overrides aprotected method, the access modifier of the overriding method must provide equal or wider visibility.
- Permitted: Overriding as
protectedorpublic. - Compilation Error: Overriding as default (package-private) or
private.
Interface Restrictions
Theprotected modifier cannot be applied to methods declared within an interface. Interface methods are implicitly public (or explicitly private as of Java 9). Attempting to declare an interface method as protected violates the JLS and will result in a compilation error.
Master Java with Deep Grasping Methodology!Learn More





