Introduction

Let’s look into the basics of inheritance in D.

We'll cover the following

Inheritance

Inheritance is defining a more specialized type based on an existing more general base type. The specialized type acquires the members of the base type and, as a result, can be substituted in place of the base type.

Inheritance is available for classes, not structs. The class that inherits another class is called the subclass, and the class that gets inherited is called the superclass, also called the base class.

There are two types of inheritance in D. We will cover the implementation of inheritance in this chapter and leave interface inheritance to a later chapter.

When defining a subclass, the superclass is specified after a colon character:

class SubClass : SuperClass {
    // ...
}

Example

To see an example of this, let’s assume that there is already the following class that represents a clock:

Get hands-on with 1000+ tech skills courses.