C# Abstract Class with Examples

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

  • Cannot be instantiated: You cannot create an object of an abstract class.
  • Can contain abstract methods: These methods must be implemented by derived classes.
  • Can contain non-abstract methods: These methods can be inherited by derived classes.
  • Can contain fields and properties: Abstract classes can define fields and properties that can be shared by derived classes.

When to Use Abstract Classes?

  • Shared functionality: When you have common functionality that should be shared among multiple derived classes.
  • Base class with default behaviour: When you want to provide a base class with a default behaviour that can be overridden by derived classes.
  • Partial implementation: When you need a base class that defines some methods without implementation, leaving it to derived classes to provide the implementation.

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

  • Code Reusability: Abstract classes allow you to define common functionality in one place, promoting code reuse.
  • Extensibility: Derived classes can extend the abstract class by adding new functionalities or overriding existing ones.
  • Maintainability: Centralizing common logic in an abstract class makes it easier to maintain and update the code.

Abstract Class vs. Interface

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

  • Abstract Classes
    • Can contain both abstract and non-abstract methods.
    • Can have fields, properties, and constructors.
    • Supports access modifiers for methods and properties.
    • Allows single inheritance (a class can inherit only one abstract class).
  • Interfaces
    • Can contain only method signatures (until C# 8.0, which introduced default implementations).
    • Cannot have fields or constructors.
    • All members are implicitly public.
    • Supports multiple inheritance (a class can implement multiple interfaces).

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.


Recommended Free Ebook
Similar Articles