Example
Explore how to build specialized subclasses and override abstract functions within a class hierarchy in D programming. Understand the principles of inheritance, the use of abstract classes for defining behaviors, and how to model real-world relationships with classes such as RailwayVehicle, Locomotive, and RailwayCar.
We'll cover the following...
Railway car example
Let’s consider a class hierarchy that represents railway vehicles:
The functions that RailwayCar will declare as abstract are indicated by question marks.
Since our goal is only to present a class hierarchy and point out some of its design decisions, we will not fully implement these classes. Instead of doing actual work, they will simply print messages.
The most general class of the hierarchy above is RailwayVehicle. In this program, it will only know how to move itself:
A class that inherits from RailwayVehicle is Locomotive, which does not have any special members yet:
RailwayCar is a RailwayVehicle ...