<aside> Abstract classes are the foundation of object-oriented design, serving as templates for other classes while never being instantiated themselves.
</aside>
An abstract class serves as a base class in class hierarchies. It cannot be instantiated directly and is meant to be inherited by other classes.
In UML diagrams, abstract classes are distinguished from regular classes with specific notation:
<aside> 💡 Example: In a UML class diagram, our Shape class would appear as Shape with CalculateArea() in italics, while concrete methods like DisplayInfo() would be in regular font.
</aside>
public abstract class Shape
{
public string Color { get; set; }
// Abstract method - must be implemented by derived classes
public abstract double CalculateArea();
// Concrete method - available to all derived classes
public virtual void DisplayInfo()
{
Console.WriteLine($"This is a {Color} shape.");
}
}
// Concrete class inheriting from Shape
public class Circle : Shape
{
public double Radius { get; set; }
// Must implement the abstract method
public override double CalculateArea()
{
return Math.PI * Radius * Radius;
}
}
<aside> This example demonstrates how abstract classes can be used to create a hierarchy of vehicle types.
</aside>