Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
public class SuperClass {
    protected void executeTask() {
        // Method implementation
    }
}

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.
package com.example.parent;

public class Parent {
    protected void initialize() {}
}
package com.example.child;

import com.example.parent.Parent;

public class Child extends Parent {
    public void testAccess() {
        // Valid: Accessing via inheritance (implicit 'this')
        initialize(); 
        
        // Valid: Accessing via instance of the subclass
        Child childInstance = new Child();
        childInstance.initialize(); 
        
        // Invalid: Compilation error. Cannot access via superclass reference 
        // from a different package.
        Parent parentInstance = new Parent();
        // parentInstance.initialize(); 
    }
}

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.
package com.example.other;

import com.example.parent.Parent;

public class SubClass extends Parent {
    // Valid: Widening visibility
    @Override
    public void initialize() {
        super.initialize();
    } 
    
    // Invalid: Narrowing visibility (Compilation Error)
    // @Override
    // void initialize() {} 
}

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.
Master Java with Deep Grasping Methodology!Learn More