Why we use abstract class
An abstract class is
used to define what is known as a base class. A base class is a class which has
the most basic definition of a particular requirement.
Understand more we take
an example
Suppose we have a two
class Permanent_Employee and contractual_employee
And both class have
name (firstName , lastName) , department, in traditional approach we create two
class then In main we call each class method
In above image we have
seen that we have common method name and variable name to minimize the code we
just write a class that have common method definition and variable name.
Now we have declared a
class Employee as a base class
class Permanent_Employee and contractual_employee
are
child class, both class inherit base class Employee
Now base class have all method and property, we
declare base class method as virtual then may be override
in child class
See the code I will explain the problem in this code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DPSG_DACK
{
public class Employee
{
static string EmpFName, EmpLName, Department;
public string FirstName { get; set; }
public string LastName { get; set; }
public string department { get; set; }
public string getName()
{
return this.FirstName + " " + this.LastName +" You are working with
"+this.department+" department";
}
public virtual double calculateSalary()
{
throw new NotImplementedException();
}
}
public class Permanent_Employee:Employee
{
public int annualsalry { get; set; }
public override double calculateSalary()
{
return this.annualsalry / 12;
}
}
public class contractual_employee:Employee
{
public int perdayRate { get; set; }
public int NoOfdays { get; set; }
public override double calculateSalary()
{
return this.perdayRate * this.NoOfdays;
}
}
class Program
{
public static void Main(string[] args)
{
Permanent_Employee PEmpObj = new Permanent_Employee()
{
FirstName = "shiv",
LastName = "Kumar",
department = "IT",
annualsalry=400000
};
Console.WriteLine("Hi MR." + PEmpObj.getName());
Console.WriteLine("You current Month salry = " +
PEmpObj.calculateSalary());
Console.WriteLine("=============================================================================");
contractual_employee CempObj = new contractual_employee()
{
FirstName = "deepak",
LastName = "Bhati",
department = "HR",
perdayRate = 200,
NoOfdays=20
};
Console.WriteLine("Hi MR." + CempObj.getName());
Console.WriteLine("You current Month salry = " +
CempObj.calculateSalary());
Console.ReadLine();
}
}
}
The above code working
fine, but in this code user have rights to create the object of base class
Employee and access the calculateSalary() method , if you see the code base class
method throw an error
public
virtual
double
calculateSalary()
{
throw
new
NotImplementedException();
}
Employee
objEmp = new Employee();
objEmp.calculateSalary();
To avoid this, we use abstract
class by the definition
“An Abstract class is never
intended to be instantiated directly.”
public
abstract
class
Employee
{
static
string
EmpFName, EmpLName, Department;
public
string
FirstName { get; set;
}
public
string
LastName { get; set;
}
public
string
department { get; set;
}
public
string
getName()
{
return
this.FirstName
+ " " + this.LastName
+" You are working with "+this.department+" department";
}
public
abstract
double
calculateSalary();
}
==================================================
It means the place where we want, our base class
method must define in the child class that inherit a base class, or
No one can create the object of base class method
directly we use abstract classes
When don’t want the base class to be instantiated
========================================================
“In Interface we can’t create a filed in base class ” that’s why we use abstract class for best result
Hope
this article is use full for you J
Thanks
Shiv
Kumar Rawat