What is an Abstract Class in C# and How to Use It?

What is an Abstract Class? After studying Abstraction in OOPs let’s talk about Abstract Class. An Abstract class is declared with the Abstract keyword and we can not create an object of an Abstract class. The Abstract classes are similar to Interfaces with few differences. For a better understanding of an Abstract class, first, we need to understand why we need the Abstract class in the first place.

Why Abstract Class?
When developing an application, we need to create some classes that we only use as a base class but we never create an object for that class. Such classes are called Abstract classes. For example, if we create an application for a school, we will have several entities in that application such as Teacher, Student, Principal etcetera.
Students, Teachers, and Principals, all of them are human and they will have some basic properties and functions. For example, they will have a name, address, phone number, and age. Whenever we create an entity in our application, this entity will have these basic properties and functions. If we keep on creating these functions and properties in every class, this will be a wastage of time. This is why we need to create an abstract class and make this abstract class a base class of other human entity classes. I hope you have understood why we create an abstract class. Microsoft has provided us Abstract Class in C# with some rules. Let’s talk about the rules of the abstract class.
Rules of Abstract Class
- An Abstract Class can only be used as a base class. Creating an object of an Abstract class is not allowed.
- It can or cannot have Abstract methods.
- It can have methods with implementation, unlike Interfaces.
- If there is an abstract method in an abstract class, it is the responsibility of the child class to provide an implementation of this Abstract method. If the implementation is not provided inside the child class, the child class also becomes an abstract class. Once the child class becomes an abstract class, you won’t be able to create an object of the child class as well.
- Override keyword is used to provide the implementation to an abstract method. Look at the example for better understanding.
- If a non-abstract class contains an abstract method, the entire class becomes an abstract class.
- It can have access modifiers for its method and properties.
- It can contain constants and fields.
- It can have constructors and deconstructors.
Example Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractClass
{
abstract class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public abstract void PrintDetails();
public void PrintBasicInfo()
{
Console.WriteLine("First Name - {0}", FirstName);
Console.WriteLine("Last Name - {0}", LastName);
Console.WriteLine("Age - {0}", Age);
Console.WriteLine("Address - {0}", Address);
}
}
class Student : Person
{
public long RollNumber { get; set; }
public int Class { get; set; }
public string Stream { get; set; }
public override void PrintDetails()
{
Console.WriteLine("First Name - {0}", FirstName);
Console.WriteLine("Last Name - {0}", LastName);
Console.WriteLine("Age - {0}", Age);
Console.WriteLine("Address - {0}", Address);
Console.WriteLine("Roll Number - {0}", RollNumber);
Console.WriteLine("Class - {0}", Class);
Console.WriteLine("Stream - {0}", Stream);
}
}
class Teacher : Person
{
public string Speciality { get; set; }
public long Salary { get; set; }
public override void PrintDetails()
{
Console.WriteLine("First Name - {0}", FirstName);
Console.WriteLine("Last Name - {0}", LastName);
Console.WriteLine("Age - {0}", Age);
Console.WriteLine("Address - {0}", Address);
Console.WriteLine("Speciality - {0}", Speciality);
Console.WriteLine("Salary - {0}", Salary);
}
}
class Program
{
static void Main(string[] args)
{
Student student = new Student()
{
FirstName = "Will",
LastName = "Smith",
Age = 18,
Address = "New York",
RollNumber = 1,
Class = 10,
Stream = "Mathematics"
};
student.PrintBasicInfo();
Console.WriteLine("***************************");
student.PrintDetails();
Console.WriteLine("*********************");
Teacher teacher = new Teacher()
{
FirstName = "John",
LastName = "Cena",
Age = 33,
Address = "Chicago",
Salary = 10000,
Speciality = "Physics"
};
teacher.PrintBasicInfo();
Console.WriteLine("************************");
teacher.PrintDetails();
Console.ReadLine();
}
}
}
Program Explanation
As you can see the Person is an abstract class that has some basic properties and methods. There is a normal method called PrintBasicInfo with implementation. There is another method called PrintDetails which is an abstract method that does not have an implementation.
The Person class is used as a base class for Student and Teacher classes because it has common properties. The Student and Teacher classes provided the implementation for the PrintDetails method as per their need and used the PrintBasicInfo method as it is because it is used to print basic info on the console.
We have used the Override keyword to provide an implementation to abstract method PrintDetails. It is not possible to provide implementation to an abstract method without using the override keyword. If you have learning inheritance in C# you will know how to make use of Override and Virtual keywords but here we make use of only the Override keyword. In case you want to learn about inheritance, click on the link above, we have already covered the topic in details.
Abstract Method
An abstract method is declared with an abstract keyword just like in the example above. An abstract method is a method in a class that does not have an implementation.
Declaration of an Abstract Method
public abstract void PrintDetails();
Such classes get implementation from the child class. The class that has an abstract method is called an Abstract class. Abstract methods are declared so that it comes to the responsibility of the child class to provide their implementation.
In the above program example, PrintDetails is an abstract method, and it will have implementation according to the Student and Teacher classes. Student and Teacher classes will have some specific properties and that is why the implementation for PrintDetails will be according to that particular class.
Implementation of an Abstract Method
public override void PrintDetails()
{
Console.WriteLine("First Name - {0}", FirstName);
Console.WriteLine("Last Name - {0}", LastName);
Console.WriteLine("Age - {0}", Age);
Console.WriteLine("Address - {0}", Address);
Console.WriteLine("Roll Number - {0}", RollNumber);
Console.WriteLine("Class - {0}", Class);
Console.WriteLine("Stream - {0}", Stream);
}
As you can see above, override keyword is used to provide the implementation to an Abstract method.
Conclusion
Abstract class concept is very helpful when developing a complicated application with a lot of entities. It saves a lot of time and effort. You don’t need to declare common properties time and again. Just create an abstract class and make it a base class. You are good to go.
I hope the article was helpful. Look at the example program for better understanding. Kindly comment if you have any confusion. I will be glad to clear your confusion.
Stay Safe Stay Healthy, Take care!