Consider a base employee class with few properties and then you create a full time and part time employee classes that inherit from the employee class.
Base Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
abstract class Employee
{
public abstract void PrintEmployee();
}
}
Derived Class 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class FullTime:Employee
{
public string EmployeeType { get; set; }
public string OverTime { get; set; }
public string VacationDays {get;set;}
public override void PrintEmployee()
{
Console.WriteLine("Employee Type={0}, OverTime={1}, VacationDays{2}",
EmployeeType, OverTime, VacationDays);
Console.WriteLine(Environment.NewLine);
}
}
}
Derived Class 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class PartTime:Employee
{
public string EmployeeType { get; set; }
public string OverTime { get; set; }
public string VacationDays {get;set;}
public override void PrintEmployee()
{
Console.WriteLine("Employee Type={0}, OverTime={1}, VacationDays{2}",
EmployeeType, OverTime, VacationDays);
Console.WriteLine(Environment.NewLine);
}
}
}
As you can see, base abstract method "PrintEmployee" is implemented in derived classes. You can declare a method signature with keyword abstract or yon can declare a fully implemented method and use it in derived classes.
If you want to implement a method like we did here, you must declare the method with keyword "abstract" in base class and use keyword "override" in derived class.
This is a very basic concept but could be confusing for someone new to object oriented programming paradigm. Hope this helps you understand the concept.
In future post we will discuss polymorphism via interfaces.
Thank you.
No comments:
Post a Comment