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 constructor in Java is a class constructor declared with the protected access modifier, restricting direct instantiation to classes within the exact same package, while permitting subclasses in any package to invoke it for superclass state initialization. It governs the visibility of the object initialization process, ensuring that unrelated classes outside the package boundary cannot directly instantiate the class.

Syntax

The protected keyword is placed directly before the constructor declaration:
public class CoreComponent {
    // Protected constructor
    protected CoreComponent() {
        // Initialization logic
    }
}

Access Rules and Mechanics

The protected modifier enforces strict rules on where the new keyword can be used to allocate memory for the class, and where the superclass constructor invocation (super()) can be used to initialize inherited state.
  1. Intra-Package Access (Same Package): Any class residing in the same package as the class with the protected constructor can instantiate it directly using the new keyword, provided the class is concrete (not abstract). In this context, it behaves identically to package-private (default) access.
  2. Inter-Package Access (Subclasses): A subclass located in a different package can access the protected constructor, but only through implicit or explicit superclass constructor invocations (super()) within its own constructor. The subclass cannot instantiate the parent class directly using the new keyword. The super() invocation does not allocate memory or create a standalone instance of the parent class; rather, it initializes the parent class portion of the newly allocated subclass instance.
  3. Inter-Package Access (Non-Subclasses): Any non-subclass located in a different package is strictly prohibited from accessing the constructor. Attempting to do so results in a compilation error (The constructor is not visible).

Code Visualization

The following multi-package example demonstrates the mechanical boundaries of the protected constructor. All uncommented code compiles successfully. Package A: The Base Class
package com.system.core;

public class BaseEntity {
    protected BaseEntity() {
        System.out.println("BaseEntity state initialized.");
    }
}
Package A: Same Package Instantiation
package com.system.core;

public class CoreHelper {
    public void createEntity() {
        // Valid: CoreHelper is in the same package.
        // The 'new' keyword allocates memory and invokes the constructor.
        BaseEntity entity = new BaseEntity(); 
    }
}
Package B: Subclass Initialization
package com.system.extended;

import com.system.core.BaseEntity;

public class ExtendedEntity extends BaseEntity {
    public ExtendedEntity() {
        // Valid: Accessed via superclass constructor invocation.
        // Initializes the BaseEntity state of the ExtendedEntity instance.
        super(); 
    }

    public void invalidCreation() {
        // INVALID: Subclasses in different packages cannot use 'new' 
        // to directly instantiate a class with a protected constructor.
        
        // BaseEntity entity = new BaseEntity(); // Compilation error
    }
}
Package B: Unrelated Class Instantiation
package com.system.extended;

import com.system.core.BaseEntity;

public class UnrelatedClient {
    public void attemptCreation() {
        // INVALID: Not in the same package, and not a subclass.
        
        // BaseEntity entity = new BaseEntity(); // Compilation error
    }
}

Inheritance Implications

When a class with a protected constructor is extended, the subclass constructor must be able to invoke the parent constructor. If the subclass does not explicitly invoke super(), the Java compiler attempts to insert an implicit super() invocation to the parent’s no-argument constructor. Because the parent constructor is protected, this implicit invocation successfully resolves during compilation across package boundaries, provided the inheritance hierarchy is valid.
Master Java with Deep Grasping Methodology!Learn More