Introduction

In object-oriented programming, abstract classes play a crucial role in defining common behaviours and characteristics for derived classes. C# provides powerful support for abstract classes, allowing developers to create robust and flexible code architectures. This article will delve into the concept of abstract classes in C#, explore their advantages, and provide practical examples to help you master their use.

What is an Abstract Class?

An abstract class in C# is a class that cannot be instantiated directly. Instead, it serves as a blueprint for other classes. Abstract classes can contain abstract methods (methods without implementation) as well as non-abstract methods (methods with implementation). Derived classes must implement the abstract methods defined in the abstract class.

Key Features of Abstract Classes

When to Use Abstract Classes?

Example Abstract Class in C#

Let's explore an example to understand how abstract classes work in C#.

Step 1. Define the Abstract Class.

First, we define an abstract class Shape with an abstract method CalculateArea and a non-abstract method Display.

public abstract class Shape
{
    public string Color { get; set; }

    // Abstract method (does not have a body)
    public abstract double CalculateArea();

    // Non-abstract method (has a body)
    public void Display()
    {
        Console.WriteLine($"The color of the shape is {Color}");
    }
}

Step 2. Create Derived Classes.

Next, we create derived classes Circle and Rectangle that inherit from the abstract class Shape and implement the abstract method CalculateArea.

public class Circle : Shape
{
    public double Radius { get; set; }

    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public override double CalculateArea()
    {
        return Width * Height;
    }
}

Step 3. Use the Derived Classes.

Finally, we use the derived classes to create objects and call their methods.

class Program
{
    static void Main()
    {
        Circle circle = new Circle { Color = "Red", Radius = 5 };
        Rectangle rectangle = new Rectangle { Color = "Blue", Width = 4, Height = 6 };

        circle.Display();
        Console.WriteLine($"The area of the circle is {circle.CalculateArea()}");

        rectangle.Display();
        Console.WriteLine($"The area of the rectangle is {rectangle.CalculateArea()}");
    }
}

Advantages of Abstract Classes

Abstract Class vs. Interface

While abstract classes and interfaces in C# can both define contracts for derived classes, they have some key differences.

Conclusion

Abstract classes are a powerful feature in C# that enables developers to create flexible and maintainable code architectures. By defining common functionality in abstract classes and requiring derived classes to implement specific methods, you can create a robust and extensible codebase.

Understanding and mastering abstract classes is essential for any C# developer looking to build scalable and maintainable applications. By following the principles and examples outlined in this article, you can leverage abstract classes to enhance your object-oriented programming skills and create more efficient and effective code.