Skip to main content
A protected method in Java is a class member defined with the 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 a protected 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 the protected 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 a protected method, the access modifier of the overriding method must provide equal or wider visibility.
  • Permitted: Overriding as protected or public.
  • Compilation Error: Overriding as default (package-private) or private.

Interface Restrictions

The protected 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.
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More