Photo by Steve Johnson on Unsplash
When to use Abstract class in C#.NET
Realtime scenario when to use abstract class in C#.NET
May be you already know what are abstract classes in C#.NET in case if you don't know let me give you a brief introduction, An abstract class in C#.NET is the class which 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 method of parent abstract class and provide functionality of the method.
When to use 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 commonProperties -> 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 Student and Teacher in Person and thats it but look at the problem in this case any one create the object of any class Student, Teacher or Person. Thats totally 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 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 and 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();
and when we call the method in the dwerrived 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 will try to Create the object of Parent class 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 parent class and want to restrict the functionality of object creation of the parent class you can use the abstract class.