Font size
Site color
Skip to main content

Abstract Classes

Abstract classes are classes that cannot be instantiated. Their purpose is to provide a class definition for classes derived from them. It can be said that abstract classes define the “behavior” that their derived classes must implement.

You declare an abstract class by specifying the abstract keyword before its definition. For example:

abstract class Person
{
    // abstract class members
}

Abstract Members of Abstract Classes

Abstract methods are declared inside abstract classes (which is also the only place where they can be declared). Abstract methods are methods declared in an abstract class that do not contain an implementation. Instead, they only define the method signature, and every concrete (non-abstract) class that inherits from this abstract class must implement those methods. To declare a method as abstract, you need to specify the abstract keyword before its return type, and then, instead of implementing its body, you terminate it with ;. For example:

abstract class Person
{
    public abstract string Name(string name);
}

Since abstract methods are not implemented in abstract classes, they must be public and must be implemented in derived classes. For example:

using System;

abstract class Person
{
    public abstract string Name(string name);
}

class Teacher : Person
{
    public override string Name(string name)
    {
        name = "Teacher: " + name;
        return name;
    }
}

class Student : Person
{
    public override string Name(string name)
    {
        name = "Student: " + name;
        return name;
    }
}

class Program
{
    static void Main()
    {
        Teacher t = new Teacher();
        Console.WriteLine(t.Name("Velimir Radlovacki"));
        Student s = new Student();
        Console.WriteLine(s.Name("Petar Petrovic"));
    }
}

In this example, classes Teacher and Student are derived from the abstract class Person. Because the public abstract method Name() is defined in the Person class, the implementation of that method must also be defined in classes Teacher and Student. When implementing the Name() method in derived classes, you must specify the override keyword before the method return type.

Executing this program creates object t of class Teacher and object s of class Student, where classes Teacher and Student are derived from the abstract class Person. Then methods t.Name and s.Name are called, as implemented in classes Teacher and Student, printing to the console:

Teacher: Velimir Radlovacki
Student: Petar Petrovic

Non-Abstract Members of Abstract Classes

Abstract classes can also contain non-abstract members — that is, methods with implementations that can be directly used by derived classes. In fact, an abstract class does not need to have any abstract methods at all. It can contain only non-abstract methods, fields, properties, and events. The goal of an abstract class is that it cannot be instantiated and serves as a foundation for other classes. This means abstract classes can contain both abstract and non-abstract members. For example:

using System;

abstract class Person
{
    public abstract string Name(string name);

    public void School()
    {
        Console.WriteLine("School Center "Nikola Tesla"");
        Console.WriteLine("Vrsac");
    }
}

class Teacher : Person
{
    public override string Name(string name)
    {
        name = "Teacher: " + name;
        return name;
    }
}

class Student : Person
{
    public override string Name(string name)
    {
        name = "Student: " + name;
        return name;
    }
}

class Program
{
    static void Main()
    {
        Teacher t = new Teacher();
        Student s = new Student();
        Console.WriteLine(t.Name("Velimir Radlovacki"));
        t.School();
        Console.WriteLine(s.Name("Petar Petrovic"));
        s.School();
    }
}

Executing this program creates objects t of class Teacher and s of class Student, where classes Teacher and Student are derived from the abstract class Person. Then methods t.Name and s.Name are called, as implemented in classes Teacher and Student, as well as methods t.School() and s.School() whose implementation is defined in the abstract class Person. The console output will be:

Teacher: Velimir Radlovacki
School Center "Nikola Tesla"
Vrsac
Student: Petar Petrovic
School Center "Nikola Tesla"
Vrsac

Abstract classes can be derived from other classes, including other abstract classes. This enables creating complex class hierarchies where each abstract class can add additional abstract or non-abstract members.