C# abstract class

The abstract class is an essential feature of .NET. The main reason behind creating an abstract class is that to use it as a base class. An abstract can only be used as a base class, and we can not create an object of abstract class.

By using abstract keyword we can make a class abstract.

abstract class Student
{
    //Class body
}

Abstract Method

An abstract class can have abstract and non-abstract members.

If a class member is marked as abstract then it can not have an implementation in the abstract class, similarly, if a class member is a non-abstract member then it must have an implementation.

By using the abstract keyword we can make a class member abstract. If a member is marked as abstract then it's access modifier must be public.

In the following example, Student class is containing an abstract and a non-abstract member.

abstract class Student
{
    public abstract void PrintDetails();
 
    void PrintMarks()
    {
        //Method body
    }
}

If a class is inheriting an abstract class then it must implement all the abstract members of it.

In the following example, StudentInfo class is inheriting the Student class, the Student class is an abstract class and it is containing an abstract and a non-abstract member. Due to the fact, StudentInfo class is implementing the PrintDetails method.

class StudentInfo : Student
{
    public override void PrintDetails()
    {
        //Method body
    }
}
 
abstract class Student
{
    public abstract void PrintDetails();
 
    void PrintMarks()
    {
        //Method body
    }
}

To implement an abstract member we use override keyword in the child class. If it's not provided, the compiler will throw an error saying abstract member does not have an implementation in the child class.

In C# it is perfectly valid to inherit an abstract class into another abstract class. It is not necessary to implement base class abstract member into the child abstract class, However, if you want you can implement an abstract member in child abstract class.

abstract class StudentInfo : Student
{
    //PrintDetails method implemented in child class
    public override void PrintDetails()
    {
        //Method body
    }
}
 
abstract class StudentMarks:Student
{
    //No implementation given
}
abstract class Student
{
    public abstract void PrintDetails();
 
    void PrintMarks()
    {
        //Method body
    }
}

Abstract properties

Like we create abstract methods we can create abstract properties as well. In the following example, Student class is having an abstract property and it is being implemented by the child class.

class StudentInfo : Student
{
    string studentName;
 
    public override string StudentFullName
    {
        get
        {
            return studentName;
        }
        set
        {
            studentName = value;
        }
    }
}
 
abstract class Student
{
    public abstract string StudentFullName { get; set; }
}

Following example demonstrates the use of an abstract class.

Example

Try It
public static void Main()
{
    StudentInfo SI = new StudentInfo();
    SI.StudentFullName = "Mark";
    SI.TotalMarks = 100;
 
    SI.PrintFullName();
    SI.PrintTotalMarks();
}

class StudentInfo : Student
{
    string studentName;
 
    public override string StudentFullName
    {
        get
        {
            return studentName;
        }
        set
        {
            studentName = value;
        }
    }
 
    public override void PrintFullName()
    {
        Console.WriteLine("Student name is :{0}", StudentFullName);
    }
}
 
abstract class Student
{
    public abstract string StudentFullName { get; set; }
    public int TotalMarks { get; set; }
 
 
    public abstract void PrintFullName();
 
    public void PrintTotalMarks()
    {
        Console.WriteLine("Student total marks :{0}", TotalMarks);
    }
}

Let's learn about the differences between abstract class and interface in the next section.