Skip to main content

Abstract Classes

Written by: Akram Taghavi-Burris | © Copyright 2024
Status

Abstract classes serve as a base template for other classes, allowing them to inherit shared behaviors while also providing the flexibility to override or customize specific aspects. This differs from implementing an interface, which mandates that all defined properties and methods be declared in the implementing class.

Abstract "Base" Class

Abstract classes are typically use the "Base" sufix to indicate that they serve as base classes for inheritance and can not be instantiated directly.

When to Implement

Abstract classes are best used when you want to provide a common foundation or shared characteristics among multiple related classes. This can include:

  • Providing Default Implementations: Abstract classes allow you to define common functionality that can be inherited by subclasses, reducing code duplication.

  • Enforcing Consistency: By declaring abstract methods, you ensure that all subclasses must implement certain behaviors, maintaining a consistent interface across different types of movable objects.

  • Creating Object Hierarchies: Abstract classes facilitate the creation of hierarchies, allowing related classes to inherit common traits while still having the flexibility to implement unique behaviors.