Hello friends, In this tutorial, I am going to explain you about Abstract Class in C# with an example. Also, we are going to discuss in which scenario we should use Abstract Class.

After completing this session you will be able to understand:

  • What is Abstract Class in C#?
  • What is an Abstract method in C#?
  • What is Concrete method in C#?
  • When to use Abstract Class in C#?

Introduction:

Before proceeding to Abstract Class let’s know about what is an Abstract method in C#?

Abstract Method:

An abstract method is a method that has an only signature, not implementation. The implementation of the Abstract method is done by the class that derives the abstract class. The derived class must override the abstract method. An Abstract method can only be defined in a class that has been declared as an Abstract class.

Abstract Class:

As now we are clear about an Abstract method. So let’s talk about Abstract class in C#. An abstract class is a class in C# that can’t be instantiated. An abstract class can have both abstract methods and Concrete method. Concrete methods are those methods that have methods with their implementation. When a class is marked as the abstract class, then we cannot create an instance of that class.

So now we are familiar with Abstract Method and Abstract Class in C#. Now the point is when to use Abstract class in C#. This is mostly asked interview question.  Let’s discuss it.

 

When to use Abstract Class?

We should use an abstract class, when we want to move the common functionality of two or more related classes into a base class and when we don’t want to create an object of that base class.

Can we create a constructor of the Abstract Class?

Yes, we can create a constructor of the Abstract class. Generally, a constructor is used to initialize a member of a class. In the same way, a constructor of an Abstract class is used to initialize the fields of the abstract. But now the question is how can we create a constructor of a class that we can’t instantiate? This is possible by a derived class that derives the abstract class. The constructor of the abstract class is atomically called when we create an instance of the derived class.

Let’s create an example using Abstract Class.

Suppose a scenario where there are two types of Employees such as Contract Employee and Fulltime Employee. Both types of employees have the common properties like their EmployeeID, EmployeeName and Address expecting their salary calculation. Contract Employee will get salary on an hourly basis, whereas Fulltime Employee will get salary on annual basis. Let’s start creating an application using C# console application.

1- Start Visual Studio and create a simple Console application like this.

Abstract Class

Step 2-  Create an EmployeeBase class. This class will be our Abstract Class.

Abstract Class

Step 3 – Write the following code in EmployeeBase class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;




namespace AbstractClassDemo

{

   public abstract class EmployeeBase

    {

       public int EmployeeID { get; set; }

       public string FirstName { get; set; }

       public string LastName { get; set; }

       public string Address { get; set; }

       public string GetBasicDetails()

       {

           return "Employee ID :" + this.EmployeeID + ",Name :" + this.FirstName + " " + this.LastName + ", Address :" + this.Address;

       }

       Public abstract double GetEmployeeMonthlySalary();

    }

}

Step 4- Create a class, FullTimeEmployee class.

Abstract Class

Step 5 –First thing you need to do is to inherit FullTimeEmployee class from EmployeeBase class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;




namespace AbstractClassDemo

{

    class FullTimeEmployee:EmployeeBase

    {

    }

}

Step 6- Here is the complete code for FulltimeEmployee class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;




namespace AbstractClassDemo

{

    class FullTimeEmployee:EmployeeBase

    {

        public double AnnualSalary { get; set; }

        public override double GetEmployeeMonthlySalary()

        {

          return  this.AnnualSalary / 12;

        }

    }

}

Step 7-  Create a class ContractEmployee.

Abstraction Class

Step 8- Write the following code in ContractEmployee class.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;




namespace AbstractClassDemo

{

    class ContractEmployee:EmployeeBase

    {

        public double HourlyPay { get; set; }

        public double TotalHours { get; set; }




        public override double GetEmployeeMonthlySalary()

        {

            return this.TotalHours * this.HourlyPay;

        }   

    }

}

Step 9- All done. So now we need to object of these two classes. Here it is.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AbstractClassDemo
{
 class Program
 {
 static void Main(string[] args)
 {
 FullTimeEmployee objFullTimeEmployee = new FullTimeEmployee();
 {
 objFullTimeEmployee.EmployeeID = 1;
 objFullTimeEmployee.FirstName = "Vinod";
 objFullTimeEmployee.LastName = "Palne";
 objFullTimeEmployee.Address = "Varanasi";
 objFullTimeEmployee.AnnualSalary = 5000000;
 }
 Console.WriteLine("----------Fulltime Employee Details-----------");
 Console.WriteLine(objFullTimeEmployee.GetBasicDetails());
 Console.WriteLine("Employee Monthly Salary :" + objFullTimeEmployee.GetEmployeeMonthlySalary());

ContractEmployee objContractEmployee = new ContractEmployee();
 {
 objContractEmployee.EmployeeID = 2;
 objContractEmployee.FirstName = "Ansh";
 objContractEmployee.LastName = "Palne";
 objContractEmployee.Address = "Varanasi";
 objContractEmployee.HourlyPay = 1000;
 objContractEmployee.TotalHours = 50;
 }
 Console.WriteLine("----------Contract Employee Details--------------");
 Console.WriteLine(objContractEmployee.GetBasicDetails());
 Console.WriteLine("Employee Monthly Salary :" + objContractEmployee.GetEmployeeMonthlySalary());
 Console.ReadKey();

}
 }
}

Step 10- Run the program and you can see the output.

Abstract Class

You can download full source code from here.

You must follow the related links.

Hope you liked this post about Abstract Class in C#. You can follow the related post here.

Thank You.

 

Leave a Comment