C# Abstract Class

Summary: in this tutorial, you’ll learn about C# abstract classes and how to use them effectively.

Introduction to C# abstract classes

An abstract class is a class declared with the abstract keyword like this:

abstract class MyClass
{
}Code language: C# (cs)

Unlike a regular class, you cannot create any instances of an abstract class. Typically, you define an abstract class for subclassing by other classes.

An abstract class may contain abstract members such as abstract methods and properties.

Abstract methods

To define an abstract method, you use the abstract keyword. Unlike a regular method, an abstract method doesn’t have an implementation.

When you subclass an abstract class that has an abstract method, you need to implement the abstract method in the subclass.

The following example shows an example of an abstract method:

abstract class MyClass
{
    public abstract string AbstractMethod();
}Code language: C# (cs)

Note that an abstract method cannot be private. In other words, its accessibility level needs to be either public or protected.

If a class contains at least one abstract method, it must be an abstract class.

Abstract properties

Like an abstract method, an abstract property consists of the abstract keyword and provides no implementation for the get or set access modifier.

abstract class MyClass
{
    public abstract string AbstractProperty { get; set; }
    public abstract string AbstractMethod();    
}Code language: C# (cs)

C# abstract class example

First, define an abstract class called Shape:

abstract class Shape
{
    public abstract double Area();

    public abstract int Side { get; }
}Code language: C# (cs)

The Shape class has an abstract method Area() that returns the area of the shape. It also has the abstract property Side that returns the number of sides of the shape.

Second, define the Circle class that inherits from the Shape class:

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

    public override int Side => 0;

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

    public override double Area()
    {
        return Math.PI * Radius * Radius;
    }
}Code language: C# (cs)

In this Circle class, we must implement both Area() method and Side property.

Since a circle has zero sides, the Side property returns 0. Also, the Area() method returns the area of the circle based on its radius (R) and PI using the formula PI * R2

Third, define the Square class that inherits from the Shape class:

class Square: Shape
{
    public double Length { get; set; }

    public override int Side => 4;

    public Square(double length)
    {
        Length = length;
    }
    public override double Area()
    {
        return Length * Length;
    }
}Code language: C# (cs)

In this Square class, the Side property returns 4. Also, the Area() method returns the area of the square based on its length.

Finally, create an array of shapes including a circle and a square, and calculate their areas:

// Program.cs

Shape[] shapes =
{
    new Square(10),
    new Circle(100)
};

foreach(Shape shape in shapes)
{
    Console.WriteLine($"The area of the {shape} is {shape.Area():0.##}");
}Code language: C# (cs)

Output:

The area of the Square is 100
The area of the Circle is 31415.93Code language: C# (cs)

Summary

  • An abstract class cannot be instantiated. It can be subclassed.
  • An abstract class may contain abstract members including abstract methods and properties.
  • Abstract methods and properties do not contain implementation.
  • The subclasses of an abstract class must implement the abstract methods and properties.
Was this tutorial helpful ?