Abstract Class in C#

Abstract Class :- Dictionary Meaning of Abstract is "Something that is Incomplete or Missing" but we are learning about the Abstract Class in the context of Programming. So we can say that Abstract Class is a Incomplete Class and it must be completed by others and it works as a Base Class. To make a Class Abstract we need to decorate that Class with Abstract Keyword as shown in Code Snippet. We can use Abstract keyword with Classes,Methods, Properties,Indexers and Events.

We can not Intantiate Abstract Class or we can say that we can not create a Object of Abstract Class by new Keyword. We only create a reference variable for it. An Abstract Class can contain Abstract and Non Abstract Members and it has implementation for some members.


                    
abstract class Voters
{

//Class with No Data Members
}

In the above code Voters is the name of class. Above line of codes will generate Compilation Error as shown in given figure and it always has Public Access Modifier with class name like.


                    public abstract  class Voters
{

//Class with No Data Members
}

Image is not available

Points to Remember :- An Abstract Class and it's members can not have a Private Access Modifier.

Abstract Class with Non Abstract Members.


                    public abstract  class Voters
{
//Non Abstract Method
public void VotersFullName(string FName,string LName)
{
string FirstName = FName;
string LastName = LName;
string FullName =Convert.ToString(FirstName + LastName);
Console.WriteLine("Voters Full Name is :"+ FullName);
}
}

In the above code we write a Non Abstract Method inside the Voters Class that is VotersFullName(string FName,string LName) with two string parameters FName and LName and Method has it's implementation for print the FullName of Voter that is provided into the parameters when Method is Called from the any other Class or Derived Class which inherits the Base Abstract Class.

How to Call Non Abstract Members from other Classes. To call a Non Abstract Method of Voters Class we need to call it from Program Class to creating a Object of Voters Class as shown into the code below.


                        class  Program
{
static void Main(string[] args)
{
Voters v = new Voters(); //it will generate a Error
v.VotersFullName("Sudhir SIngh", "Chouhan");
Console.ReadKey();
}
}

when you want to create a Object of Voters Class with new Keyword it will generate a Error because We can not create a Object of Abstract Class Directly. Error that will produce by code is given into the image below

Image is not available

To solve above error we have to inherit Voters Class by Program Class as shown in below code and we have to create a Object of Derived Class that is Program and call the Non Abstract Methods of Abstract Class.


                        class  Program:Voters  //Inherits the properties and Methods of Voters Class
{
static void Main(string[] args)
{
Program p = new Program();
p.VotersFullName("Sudhir SIngh", "Chouhan");
Console.ReadKey();
}
}

Points to Remember :-> We can not Instantiate an Abstract Class or we can not create Object of it.

Abstract Members of Abstract Class and their implementation into other Classes.


                    public abstract  class Voters
{
//Non Abstract Method
public void VotersFullName(string FName,string LName)
{
string FirstName = FName;
string LastName = LName;
string FullName =Convert.ToString(FirstName + LastName);
Console.WriteLine("Voters Full Name is :"+ FullName);
}
//This is abstarct method it does not Implementation
public abstract void VotersPollingDetails();
}

In the above code we used VotersPollingDetails() Method with Abstract keyword so it is a Abstract Member of the class and we can not provides implementation for Abstract Methods into the Abstract Class only class which inherits the Abstract Class have to provide implementation for the Abstract Method but it is not applicable on the Non Abstract Methods. We will have to Override the Abstract Method into the derived class where it is implemented. VotersPollingDetails() is a Abstract Method into the base Abstract Class so we will have to Override this methods into the derived class that is Program class as like this:


                        class  Program:Voters
{
static void Main(string[] args)
{ Program p = new Program();
p.VotersFullName("Sudhir SIngh", "Chouhan");
p.VotersPollingDetails();
Console.ReadKey();
}
//Here we Override the VotersPollingDetails() methods
public override void VotersPollingDetails()
{
Console.WriteLine("Polling Station for You is: MGM High School Street No 4 with Polling Station Number is 586");
}
}

If you miss the Override Keyword then you will get a Compilation Error like shows in the given figure.

Image is not available

Points to Remember :-

  • An Abstract Class can have both Abstract and Non Abstract Members into it.
  • Abstract Members can only be declared inside the Abstract Class and Abstract Members can not be Static or Private.
  • An Abstract Class can never be a Static Class or Sealed Class.
  • An Abstract Method can not be a Virtual.
  • A Concrete Class can not inherit more than one Abstract Class or we can say that multiple inheritance is not possible with Abstract Classes it is Possible with Interfaces only.

Image is not available

About the Author
Sudheer Singh Chouhan is a Software Engineer having Expertise in Development Design and Architecting the applications , Project Management , Designing Large Scale Databases in SQL Server since last 17 Years.
Skill Sets :- Microsoft .NET technologies like ASP.Net Core, Web API, LINQ, Web Forms, WinForms, SQL Server, EntityFramework, Design Patterns, Solid Principles, Microservices, AWS Cloud.