Abstract Classes

Learn to define abstract classes and members to enforce design contracts and share common logic across derived types.

C# includes abstract classes alongside concrete classes. An abstract class is a class that cannot be instantiated. It can hold shared functionality for its derived classes.

Abstract classes are useful when we need to define shared behavior without creating instances of the base concept. They serve as a blueprint, allowing us to share common logic while enforcing a design contract that requires derived classes to handle specific implementation details.

Creating an abstract class

Consider the Vehicle, Car, and Motorcycle classes. The Vehicle class holds members common to all vehicles. A vehicle is an abstract concept, while cars and motorcycles are concrete entities.

We mark the Vehicle class as abstract to prevent it from being instantiated directly.