# Access Specifiers in Inheritance

> Access specifiers in inheritance are the public, protected and private modes a C++ derived class can inherit with, setting the maximum access level of inherited members. Public inheritance keeps public members public, protected inheritance makes them protected, and private inheritance makes them private, while the base class's private members are never directly accessible in any mode.

Canonical: https://devsunite.com/resources/core-subjects/oop/access-specifiers-in-inheritance
Last updated: 2026-11-22

Subject: OOP · Category: Inheritance · 3 min read

Access specifiers control visibility of class members, and when you inherit a class, the specifier you inherit with (public, protected, or private) determines how the base class's members are re-exposed through the derived class. This is a C++-specific concept; Java and C# only support public inheritance for classes, so this distinction mainly matters there for member visibility, not inheritance itself.

## How it works

Each C++ inheritance mode changes the maximum access level of inherited members in the derived class:

| Inheritance type | public members become | protected members become | private members |
|---|---|---|---|
| public | public | protected | not accessible |
| protected | protected | protected | not accessible |
| private | private | private | not accessible |

Private members of the base class are never directly accessible in the derived class regardless of the inheritance mode, they can only be reached through public or protected methods the base class provides.

## A worked example

```cpp
#include <iostream>
using namespace std;

class Base {
public:
    int publicVar = 1;
protected:
    int protectedVar = 2;
private:
    int privateVar = 3;
};

class PublicDerived : public Base {
public:
    void show() {
        cout << publicVar << " " << protectedVar << endl;
        // privateVar is NOT accessible here
    }
};

class PrivateDerived : private Base {
public:
    void show() {
        cout << publicVar << " " << protectedVar << endl;
        // both are now private to PrivateDerived, invisible to anyone using PrivateDerived
    }
};

int main() {
    PublicDerived pd;
    cout << pd.publicVar << endl;   // works, publicVar is still public
    // pd.protectedVar;             // error, protected isn't accessible outside the class

    PrivateDerived prd;
    // prd.publicVar;                // error! publicVar became private through private inheritance
    return 0;
}
```

Notice that `PublicDerived::publicVar` is still reachable from `main()`, but if `Base` were inherited privately, that same `publicVar` would become private inside `PrivateDerived` and `main()` could no longer touch it at all.

## Where this matters in real systems

Public inheritance is overwhelmingly the most common case in real codebases because it models a genuine "is-a" relationship and preserves the base class's public interface, which is what polymorphism relies on. Private inheritance is rarer but shows up when a class wants to reuse a base class's implementation internally without exposing that relationship to callers at all, functionally similar to composition but occasionally used when access to protected members is specifically needed.

## What interviewers actually ask about access specifiers in inheritance

- **"What's the default inheritance mode in C++ for `class`, and for `struct`?"** Private for `class`, public for `struct`, if you write `class D : Base` with no keyword, it's private inheritance.
- **"Can a derived class access a private member of its base class?"** No, never, regardless of the inheritance access specifier used.
- **"When would you use private inheritance instead of composition?"** When you need access to the base class's protected members or need to override its virtual functions, which composition alone can't give you.
- **"Does Java have the equivalent of protected or private inheritance?"** No, Java classes only support public inheritance via `extends`; member-level access modifiers (public, protected, private, default) are Java's tool for controlling visibility instead.

## Key takeaways

- C++ has three inheritance access specifiers: public, protected, and private, each changing how base class members are re-exposed.
- Public inheritance preserves the base class's access levels and is the standard choice for real "is-a" relationships.
- Private members of the base class are never directly accessible in a derived class, no matter the inheritance mode.
- Java and C# only support public inheritance for classes, they use member-level access modifiers instead for finer control.

## Related

- [Previous: Types of Inheritance](https://devsunite.com/resources/core-subjects/oop/types-of-inheritance)
- [Next: Constructor and Destructor Order in Inheritance](https://devsunite.com/resources/core-subjects/oop/constructor-and-destructor-in-inheritance)
- [More in Inheritance: Overview of Inheritance](https://devsunite.com/resources/core-subjects/oop/overview-of-inheritance)
- [More in Inheritance: Super and Sub Classes](https://devsunite.com/resources/core-subjects/oop/super-and-sub-classes)
- [More in Inheritance: Virtual Base Classes](https://devsunite.com/resources/core-subjects/oop/virtual-base-classes)
- [More in Inheritance: Composition vs Aggregation vs Inheritance](https://devsunite.com/resources/core-subjects/oop/composition-vs-aggregation-vs-inheritance)
- [All 72 OOP interview questions](https://devsunite.com/resources/core-subjects/oop)
