What is the need of method overriding ???
When we need to extend the Parents class method's behavior with some modifications.
This is the situation where we inherit the class and override the methods which are required as per our business requirement.
namespace testApp
{
abstract class Employee
{
public int
EmployeeID { get; set; }
public String EmployeeName { get; set; }
public double
BasicSalary { get; set; }
abstract public void CalculateSalary();
}
class PermanentEmployee : Employee
{
public override void
CalculateSalary()
{
double HRA = 20 * BasicSalary / 100;
double SpecialAllowance = 10 * BasicSalary / 100;
double total = BasicSalary + HRA + SpecialAllowance;
Console.WriteLine("Salary of {0} is :{1}rs.", EmployeeName, total);
}
}
class ContractEmployee : Employee
{
public double
NoOfHours { get; set; }
//extend the
Parents class method's behaviour with
some modifications.
public override void
CalculateSalary()
{
double total = NoOfHours * BasicSalary;
Console.WriteLine("Salary of {0} is :
{1}rs.", EmployeeName, total);
}
}
class Program
{
static void Main(string[] args)
{
PermanentEmployee permanentEmployee = new PermanentEmployee();
permanentEmployee.EmployeeName = "PermanentEmployee";
permanentEmployee.BasicSalary =
25000;
permanentEmployee.CalculateSalary();
Console.WriteLine();
ContractEmployee contractEmployee = new ContractEmployee();
contractEmployee.EmployeeName = "ContractEmployee";
contractEmployee.BasicSalary =
5000;
contractEmployee.NoOfHours = 40;
contractEmployee.CalculateSalary();
Console.ReadLine();
}
}
}
Comments
Post a Comment