Protected members sit between private and public access: a class can use them, and its derived classes can read or change them. That flexibility helps subclasses extend shared behavior, though careful design still matters—too much direct access can make a class hierarchy harder to maintain.

Multiple Choice

Can a derived class modify the protected members of a base class in OOP?

In object-oriented programming, a derived class does indeed have the ability to access and modify the protected members of its base class. The concept of "protected" access indicates that these members are accessible not only within the class they are declared in but also by any subclasses that inherit from that class. This access allows derived classes to implement or extend functionality while maintaining the encapsulation principles of the base class. For example, if a base class has a protected variable such as `int count;`, a derived class can directly access and change the value of `count` as needed. This feature promotes flexibility and code reuse since derived classes can build upon the foundational logic established in the base class while still adhering to the constraints provided by the object's design. Understanding this allows programmers to design their class hierarchies effectively, ensuring that derived classes can both utilize and modify the shared state defined by their base classes while respecting the principles of inheritance.

Protected Members: How Derived Classes Work with Inherited State

Inheritance lets a class build on another class’s code. A derived class can reuse a base class’s data and behavior, then add details of its own. But reuse raises a design question: how much of the base class should a child class be able to see? In C++, the protected access level offers a middle ground. It keeps a member out of reach of ordinary outside code while allowing derived classes to access it—and, when it’s modifiable, change it.

That access can be useful, but it comes with a responsibility. A subclass that changes inherited state needs to preserve the rules the base class was designed to enforce. To see why, it helps to look at what protected means and how it differs from private and public.

A quick guide to access levels

C++ uses access specifiers to control where class members can be used:

  • public members can be accessed by code outside the class, subject to the object’s availability.

  • private members can be accessed by the class that declares them, but not directly by derived classes or outside code.

  • protected members can be accessed by the declaring class and its derived classes, but not by ordinary outside code.

For example:


class Counter {

protected:

int count = 0;

public:

int value() const {

return count;

}

};

Here, count is protected. Code that creates a Counter object can call value(), but it can’t simply write myCounter.count. A class derived from Counter, however, can refer to the inherited count member in its own code.


class StepCounter : public Counter {

public:

void advance() {

++count;

}

};

When advance() runs, it changes the inherited value. So, yes: a derived class can access and modify a protected base-class member, as long as the member itself can be modified. A member declared const, for instance, can’t be changed just because it’s protected.

How inheritance makes that possible

A derived class is a specialized kind of its base class. A StepCounter is a kind of Counter, so it can build on the state and behavior that Counter provides. Protected access gives it a way to do that without making the same state freely available to every caller.

Think of a base class as a workshop with a staff-only area. Public members are like the service counter: anyone using the class can interact with them. Private members are locked inside a cabinet that only the base class can open. Protected members are in a work area shared with trusted subclasses. They can use what’s there, but the general public still can’t walk in and alter things.

That distinction supports code reuse. A base class might hold shared state, while several derived classes use it in different ways. For example, a general Employee class might store a protected hoursWorked value. A derived HourlyEmployee class could use that value when calculating pay. The shared field avoids duplicating the same data in several classes.

Still, inheritance is not a free pass to disregard the base class’s design. If a member has rules around how it should change, a direct update may skip those rules. That’s where a protected data field can become a sharp tool: handy in the right hands, but easy to misuse.

Protected doesn’t mean public

A common point of confusion is the idea that a protected member becomes public to a derived class. It doesn’t. The subclass gets access within its own member functions, but code outside the class hierarchy still can’t access that member directly.

For example:


StepCounter steps;

steps.advance();       // Public function: allowed

// steps.count = 10;    // Error: count is protected

The advance() function is public, so callers can use it. The count field remains protected, so callers can’t set it directly. This lets the derived class provide a controlled way to update the value without exposing the field itself.

There’s also a C++ rule worth knowing: a derived class generally accesses a protected base member through an object of its own derived type, not through an unrelated base-class object. In other words, a member function in StepCounter can use count as part of itself, but it can’t use protected access as a loophole to reach into every Counter object it happens to encounter. This keeps inheritance access tied to the derived class’s own part of the class hierarchy.

The role of inheritance type

C++ also lets a class inherit using public, protected, or private inheritance:


class StepCounter : public Counter {

// ...

};

The access level after the colon affects how inherited public and protected members appear to users of the derived class. With public inheritance, base-class public members remain public in the derived class, and base-class protected members remain protected. This is the usual choice when the derived class truly represents a more specific kind of the base class.

Protected inheritance and private inheritance change that visibility. They can make inherited members less accessible through the derived class, even though the derived class itself may still use them. This is separate from the access level written on a member’s declaration. A protected field in the base class and the inheritance mode both matter, so it’s useful to read the full class declaration rather than focusing on one keyword alone.

For many everyday class hierarchies, public inheritance is the clearest model. The others have narrower uses and are best chosen for a specific design reason.

Flexibility has a trade-off

Protected members can make a class hierarchy flexible. A derived class can extend the base class’s behavior without duplicating its state or asking the base class to provide a separate function for every small variation.

But that flexibility can make the base class harder to change. If several subclasses rely directly on a protected field’s name, type, and meaning, changing that field may require updates across the whole hierarchy. The base class and its children become closely connected.

Consider a BankAccount class with a protected balance. A derived class might directly subtract money from it. But what if the account has rules about minimum balances, transaction records, or fraud checks? A direct change could bypass those rules. A public or protected method such as withdraw() can keep the update in one place, where the class can check the amount and record what happened.

This is why many designers prefer keeping data private and offering methods that describe allowed actions. For instance, addItem() says more than “change this number.” It can verify that the new value makes sense. A protected field can still be the right choice when subclasses genuinely need direct access, but it should be a deliberate decision—not a shortcut used by default.

A useful design question

When deciding whether to make a base-class member protected, ask what a derived class needs to do. Does it need the raw data, or does it only need a way to request an action? If it only needs the action, a method may give the base class better control. If subclasses need to build on shared state and direct access fits the design, protected can be a sensible tool.

So the central idea is straightforward: a derived class can access and modify a protected member inherited from its base class, within the limits of C++ access rules and the member’s own type. Protected access keeps that member unavailable to ordinary outside code, while giving subclasses room to extend the base class. Used thoughtfully, it supports reuse. Used carelessly, it can tie a class hierarchy into knots.