100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

What is a Protected Constructor?

Understand protected constructors — access rules, super() chaining and base-class design — with Java examples and interview questions.

mediumQ218 of 226 in Object Oriented Programming Est. time: 5 minsLast updated:
Open Code Lab

Expected Interview Answer

A protected constructor is a constructor accessible only to the class itself, its subclasses, and (in Java) other classes in the same package, so it can be used for controlled inheritance-based initialization while blocking arbitrary outside instantiation.

This access level sits between private (no outside access at all) and public (anyone can instantiate). A protected constructor is common on abstract base classes: the base class is never meant to be instantiated on its own, but its subclasses need to call super(...) to run the shared initialization logic. Unrelated classes outside the package cannot construct the base type directly, which enforces the design intent that the type only makes sense as a foundation for subclassing. It gives library authors a way to say "this class exists to be extended, not instantiated directly, except by trusted code nearby."

  • Allows subclasses to call super(...) for shared initialization
  • Blocks unrelated external code from instantiating a base type directly
  • Signals design intent that a class exists to be extended
  • Still permits same-package helper or factory classes controlled access

AI Mentor Explanation

A national academy’s core fitness and technique curriculum can only be adopted by an official state academy that formally joins the federation’s structure — a random private coaching camp outside the federation cannot simply plug into that curriculum. But once a state academy is affiliated, it fully inherits and builds on that base training program. A protected constructor works the same way: only subclasses within the trusted hierarchy (or the same package) can invoke it via super(), while unrelated outside code is shut out entirely.

Step-by-Step Explanation

  1. Step 1

    Mark the constructor protected

    Use the protected modifier so access is limited to the class, its subclasses, and same-package classes (in Java).

  2. Step 2

    Put it on a base or abstract class

    Most commonly used on classes designed purely to be extended, not instantiated directly.

  3. Step 3

    Subclasses call super(...)

    Each subclass constructor invokes the protected base constructor to run shared initialization logic.

  4. Step 4

    Unrelated external code is denied

    A class outside the package with no inheritance relationship cannot call new BaseClass(...) directly.

What Interviewer Expects

  • Correct placement of protected between private and public/package-private in the access hierarchy
  • Mention that subclasses use super(...) to reach a protected constructor
  • Awareness that in Java, protected also grants same-package access, not just subclass access
  • A realistic example, typically an abstract base class not meant for direct instantiation

Common Mistakes

  • Assuming protected means “subclasses only,” forgetting Java also grants same-package access
  • Confusing a protected constructor with making the whole class abstract (they are complementary, not the same thing)
  • Believing a protected constructor fully prevents instantiation, when same-package code can still call it directly
  • Not realizing an abstract class needs no access modifier trick at all to prevent instantiation, since abstract already blocks new on it directly

Best Answer (HR Friendly)

A protected constructor sits between private and public — it lets subclasses and classes in the same package create or initialize an object, but blocks unrelated outside code from instantiating it directly. It’s typically used on base classes that are meant to be extended, so the shared setup logic is available to subclasses via super, without exposing the base type to be instantiated on its own by unrelated callers.

Code Example

Protected constructor on a base class
public class Vehicle {
    protected final String vin;

    // Protected: subclasses and same-package classes can call this via super()
    protected Vehicle(String vin) {
        this.vin = vin;
    }
}

public class Car extends Vehicle {
    private final int doors;

    public Car(String vin, int doors) {
        super(vin); // subclass reaches the protected constructor
        this.doors = doors;
    }
}

// Outside the package, unrelated code cannot do: new Vehicle("XYZ123");

Follow-up Questions

  • How does protected access differ from private and public in Java?
  • Why would a base class use a protected constructor instead of a public one?
  • Does an abstract class need a protected constructor to prevent direct instantiation?
  • Can a same-package class that is not a subclass call a protected constructor?

MCQ Practice

1. A protected constructor in Java is accessible to?

In Java, protected grants access to the declaring class, its subclasses, and other classes in the same package.

2. A protected constructor is most commonly found on?

Base classes meant only to be subclassed often use protected constructors so subclasses can call super(...).

3. How does a subclass invoke its parent's protected constructor?

super(...) inside the subclass constructor invokes the parent's protected (or accessible) constructor.

Flash Cards

Protected constructor in one line?Accessible to the class, its subclasses, and same-package classes; blocked for unrelated outside code.

Where does it sit in the access hierarchy?Between private (most restrictive) and public (least restrictive).

Typical use case?Base classes meant to be extended, so subclasses can call super(...).

Common misconception?That protected means subclasses only — same-package classes also get access in Java.

1 / 4

Continue Learning