Inheritance is a way to create a new class from an existing one. The new class takes on the existing class’s properties and behaviors, and it can add features of its own. Inheriting from the existing class does not change it, so the shared code is written once and reused.
The definition
In programming, inheritance means that one class can be built on another. The new class starts with everything the existing class provides, and it can add new members. Where it makes sense, it can also change a behavior the existing class provides, which is called overriding.
Key terms
- Base class: the existing class that other classes build on. It is also called a parent class or superclass.
- Derived class: the new class built on the base class. It is also called a child class or subclass.
The exact names vary a little between languages and textbooks, but these are the common ones. If you see “parent” and “child” in one place and “superclass” and “subclass” in another, they describe the same relationship.
The is-a test
A simple way to check whether inheritance fits is the is-a test. Say “B is an A” out loud, where A is the base class and B is the derived class. If the sentence sounds right every time, inheritance may fit.
- A square is a shape, so a
Squareclass can inherit from aShapeclass. - A car has an engine, which is a has-a relationship. A
Carclass should hold anEngineas a member instead of inheriting from it.
Common forms
Textbooks usually list five common forms of inheritance:
- Single: one derived class has one base class.
- Multiple: one derived class has more than one base class.
- Multilevel: a chain, where a class inherits from a class that itself inherits from another.
- Hierarchical: several derived classes share one base class.
- Hybrid: a mix of two or more of the forms above.
Why programmers use it
- Reuse. Shared code is written once in the base class.
- Shared interface. Every derived class offers the same base behavior, so related classes behave consistently.
- Substitution. Code written for the base type can work with any derived type. Polymorphism in C++ builds on this idea.
Substitution is one of the SOLID Principles in C++, and it is a useful test for whether a derived class really fits its base.
Downsides and when to prefer composition
Inheritance links classes together. If you change the base class, every derived class may be affected, which can make changes harder. Deep hierarchies are also harder to read and follow.
When the relationship is has-a or uses-a, composition is often clearer. Composition means a class holds an object of another class as a member. Encapsulation in C++ covers how the members of a class are hidden and exposed, which matters in both approaches.
A minimal C++ sketch
In C++, the basic form looks like this:
class Child : public Parent {
};
The : public Parent part says that Child is derived from Parent, and that the public members of Parent stay public in Child. The full guide to inheritance in C++ covers the rest of the syntax.