Abstrat Class


In this article we are going to discuss the Abstract class in C#. There are many blogs, articles are available on the internet regarding Abstract class but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Abstract class in C#.

Abstract Class Introduction

Abstract classes are useful when you want to define a common structure and behavior for related classes, enforce implementation of certain methods, promote code reuse, and achieve polymorphism. These are the some important points related to the Abstract Class.

  1. Abstract classes are declared using the abstract keyword.
  2. We cannot create an object of an abstract class.
  3. It must be inherited in a subclass if you want to use it.
  4. An Abstract class contains both abstract and non-abstract methods.
  5. The methods inside the abstract class can either have an or no implementation.
  6. We can inherit two abstract classes; in this case, implementation of the base class method is optional.
  7. An Abstract class has only one subclass.
  8. Methods inside the abstract class cannot be private.
  9. If there is at least one method abstract in a class, then the class must be abstract.

The following class definition defines a abstract class in C#:

//abstract class
abstract class AbstractClass
{
}

Why Abstract Classes?

Abstract classes in C# are like blueprints for other classes. Imagine you're building different types of cars, like sports cars and trucks. You'd have a basic plan that includes things like wheels, engine, and seats, right? That's an abstract class. It's a plan that says, any car made from this blueprint must have these basic parts.

But here's the twist, you can't actually build a car directly from this blueprint. It's just a template. You need to create a specific type of car, like a sports car or a truck, based on this blueprint. Those specific cars are called concrete classes. They take the abstract plan and fill in the details to make a real car.

So, abstract classes are handy because they provide a common set of features that all related classes can share. They help keep things organized and make sure every class that follows the blueprint has the same basic structure.

Reason to Use Abstract Class

Typically, you would use a Abstract class when you want to prevent further derivation for one of two reasons:

  1. Common Functionality - Abstract classes allow you to define common methods and properties that are shared among multiple related classes.
  2. Extensibility - Abstract classes can be extended and built upon to create more specialized subclasses.
  3. Enforcing Structure - Abstract classes can define a blueprint for how subclasses should be structured. By providing abstract methods that subclasses must implement, abstract classes ensure that derived classes follow a specific pattern.

For Example

Here we create a abstract class shape and inherit that class from derived class circle and Rectangle.

using System;

// Abstract class representing a basic shape
abstract class Shape
{
    // Abstract method to calculate area
    public abstract double CalculateArea();
}

// Concrete class representing a circle
class Circle : Shape
{
    public double Radius { get; set; }

    public Circle(double radius)
    {
        Radius = radius;
    }

    // Implementation of CalculateArea for circle
    public override double CalculateArea()
    {
        return Math.PI * Radius * Radius;
    }
}

// Concrete class representing a rectangle
class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    // Implementation of CalculateArea for rectangle
    public override double CalculateArea()
    {
        return Width * Height;
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Creating instances of circle and rectangle
        Circle circle = new Circle(5);
        Rectangle rectangle = new Rectangle(4, 6);

        // Calculating and printing areas
        Console.WriteLine($"Area of circle: {circle.CalculateArea()}");
        Console.WriteLine($"Area of rectangle: {rectangle.CalculateArea()}");
    }
}

In the Main method, we create instances of Circle and Rectangle, and we can call the CalculateArea() method on each instance to get their respective areas.

Abstract Class
Next