Abstract Classes
Learn to define abstract classes and members to enforce design contracts and share common logic across derived types.
We'll cover the following...
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. This concept is a core pillar of abstraction in C#. By using abstract classes, we can hide the complex implementation details of a base concept and only expose the essential features that derived classes must have.
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.