Inheritance
Public Inheritance
Private Inheritance
Protected Inheritance
Programming Concepts

What is the difference between public, private, and protected inheritance?

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD

In object-oriented programming (OOP), inheritance is a mechanism where one class acquires the properties (methods and fields) of another. With inheritance, you can create a new class based on an existing class. The existing class is called the base class or superclass, and the new class is called the derived class or subclass. Inheritance is one of the core concepts in OOP because it allows for the creation of a more complex, yet easily manageable code base.

However, not all inheritance is created equally. In languages like C++ (and to some extent in other languages with similar models like Java or C#), you can specify how a class inherits from another class by using different types of inheritance: public, private, and protected. The type of inheritance impacts the accessibility of the superclass's members in the subclass.

Public Inheritance

Public inheritance is the most common form of inheritance. When a class inherits publicly from a base class, the public members of the base class remain public in the derived class, and protected members of the base class remain protected in the derived class.

cpp
1class Base {
2public:
3    int x;
4protected:
5    int y;
6private:
7    int z;
8};
9
10class Derived : public Base {
11    // x is public
12    // y is protected
13    // z is not accessible from Derived
14};

In this example, Derived can access Base's member x directly and y through member functions or inside the class definition, but not z.

Protected Inheritance

In protected inheritance, the public and protected members of the base class are treated as protected in the derived class. This means they can't be accessed through an instance of the derived class but can be accessed by further derived classes of the derived class.

cpp
1class Base {
2public:
3    int x;
4protected:
5    int y;
6};
7
8class Derived : protected Base {
9    // x is protected
10    // y is protected
11};
12
13class SubDerived : public Derived {
14    // Can access x and y
15};

In this setup, even though SubDerived inherits publicly from Derived, it can access x because Derived inherited it as a protected member from Base.

Private Inheritance

In private inheritance, both the public and protected members of the base class are treated as private in the derived class. That means they are not accessible by any object of the derived class nor by classes that inherit from the derived class, except through member functions.

cpp
1class Base {
2public:
3    int x;
4protected:
5    int y;
6};
7
8class Derived : private Base {
9    // x is private
10    // y is private
11};
12
13class SubDerived : public Derived {
14    // Cannot access x or y
15};

Summary Table

Type of InheritancePublic Member AccessProtected Member AccessPrivate Member Access
PublicPublicProtectedNo Access
ProtectedProtectedProtectedNo Access
PrivatePrivatePrivateNo Access

Choosing the Right Type of Inheritance

The choice of inheritance type depends on the intended use case:

  • Public Inheritance is used when the derived class is a subtype of the base class. This is the most natural form of inheritance and adheres to the “is-a” relationship.
  • Protected Inheritance might be chosen when you want to protect the base class members from being accessed by anything other than further derived classes.
  • Private Inheritance is used when you want to use the functionality of the base class but not its interface. This kind of inheritance can be considered when the relationship is more about "has a" rather than "is a".

Understanding these distinctions is crucial for designing robust and maintainable object-oriented systems. Each form of inheritance serves different design purposes, and choosing the appropriate type can lead to more intuitive and less error-prone code.


Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Object-Oriented Design practice on Codemia

Turn requirements into classes, and defend the design, on the problems that come up in OOD rounds.

Practice OOD

All Rights Reserved.