Maybe you already know what abstract classes are in C#.NET. In case you don't know, let me give you a brief introduction, an abstract class in C#.NET is a class that contains the methods declared but not defined - and you define those methods in the child classes of that abstract class. The child class will override the parent abstract class method and provide the method's functionality.
When to use an abstract class?
Let me put a scenario in front of you, You are creating software for school, and you have two classes, Student and Teacher, as you can see below.
Teacher Class
public class Teacher {
public int ID {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public string Subject {
get;
set;
}
public int MonthlySalary {
get;
set;
}
public string GetFullName() {
return "Teacher " + this.FirstName + " " + this.LastName;
}
public void AnnualSalary() {
Console.WriteLine(MonthlySalary * 12);
}
}
Student Class
public class Student {
public int ID {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public string Semester {
get;
set;
}
public string GetFullName() {
return "Student " + this.FirstName + " " + this.LastName;
}
public double GetPercentage(double ObtainMarks, double TotalMarks) {
double final_result = (ObtainMarks / TotalMarks) * 100;
return final_result;
}
}
Now these two classes have a few things in common Properties -> ID, FirstName, LastName
Method -> GetFullName() - Although the implementation is different for it in both classes.
So the Teacher and Student are person at first, so we can create a class Person.cs and put the common properties and behavior there.
Person.cs
public class Person {
public int ID {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public virtual string GetFullName() {
throw new NotImplementedException();
}
}
Note: We created the GetFullName()
as virtual because we will define this method differently in Teacher and Student classes.
Now the whole code with Student, Teacher, and Person will look like this
public class Person {
public int ID {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public virtual string GetFullName() {
throw new NotImplementedException();
}
}
//-------------------------------------------------------------------------
public class Teacher: Person {
public string Subject {
get;
set;
}
public int MonthlySalary {
get;
set;
}
public string GetFullName() {
return "Teacher " + this.FirstName + " " + this.LastName;
}
public void AnnualSalary() {
Console.WriteLine(MonthlySalary * 12);
}
}
//--------------------------------------------------------------------------
public class Student: Person {
public string Semester {
get;
set;
}
public string GetFullName() {
return "Student " + this.FirstName + " " + this.LastName;
}
public double GetPercentage(double ObtainMarks, double TotalMarks) {
double final_result = (ObtainMarks / TotalMarks) * 100;
return final_result;
}
}
The problem in Inheritance without abstract class:
We use the simple inheritance concept put the common behavior and properties of the Student and Teacher in Person, and that's it, but look at the problem, in this case, anyone creates the object of any class Student, Teacher, or Person. That is fine for Student or Teacher but not for Person. If we create the object of the person class and call the method GetFullName(), it will give the error.
public class Program {
static void Main() {
Person person = new Person();
person.GetFullName();
}
}
Error: Unhandled exception.System.NotImplementedException: The method or operation is not implemented.
And there is no reason to create the object of the Person class, so in this case, we make the class Person abstract as we are already defining the GetFullName()
for both Teacher and Student separately, we don't want to create the object of the person class as well, so the solution for these problems is Abstract class.
To make a method abstract, we use the word abstract in the method declaration like public abstract string GetFullName();.
When we call the method in the derived classes, i.e., Student and Teacher, we will use the word override. below is the complete code with the abstract class implementation
public abstract class Person {
public int ID {
get;
set;
}
public string FirstName {
get;
set;
}
public string LastName {
get;
set;
}
public abstract string GetFullName();
}
//----------------------------------------------------------------------
public class Teacher: Person {
public string Subject {
get;
set;
}
public int MonthlySalary {
get;
set;
}
public override string GetFullName() {
return "Teacher " + this.FirstName + " " + this.LastName;
}
public void AnnualSalary() {
Console.WriteLine(MonthlySalary * 12);
}
}
//-----------------------------------------------------------------------
public class Student: Person {
public string Semester {
get;
set;
}
public override string GetFullName() {
return "Student " + this.FirstName + " " + this.LastName;
}
public double GetPercentage(double ObtainMarks, double TotalMarks) {
double final_result = (ObtainMarks / TotalMarks) * 100;
return final_result;
}
}
Now, if I try to create the Parent class object, I will get the error - but I can use the Student and Teacher class, as you can see in the ScreenShot Below.
The other benefit we are getting from it is all the derived methods are forced to implement the abstract method of the Parent abstract class i.e. GetFullName().
Summary
When you want to force derived classes to implement the methods of the parent class and want to restrict the functionality of object creation of the parent class, you can use the abstract class.