Consider a base employee interface with signature and then you create a full time and part time employee classes that inherit from this interface.
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
interface iEmployee
{
void PrintEmployee();
}
}
Derived Class 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class FullTime : iEmployee
{
public int employeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmployeeType { get; set; }
public string OverTime { get; set; }
public string VacationDays { get; set; }
// a method signature declared in interface is implemented here
void iEmployee.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:iEmployee
{
public int employeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmployeeType { get; set; }
public string OverTime { get; set; }
public string VacationDays {get;set;}
// a method signature declared in interface is implemented here
void iEmployee.PrintEmployee()
{
Console.WriteLine("Employee Type={0}, OverTime={1}, VacationDays{2}",
EmployeeType, OverTime, VacationDays);
Console.WriteLine(Environment.NewLine);
}
public void Print()
{
Console.WriteLine("Employee Type={0}, OverTime={1}, VacationDays{2}",
EmployeeType, OverTime, VacationDays);
Console.WriteLine(Environment.NewLine);
}
}
}
As you can see, a method signature "PrintEmployee" is implemented in derived classes. A class can implement one or more interfaces and must implement the method signatures declared in each interface.
You can use the above classes as follows. Notice since the method PrintEmployee is declared in an interface, you cannot use it like you would if it was declared in a class. Since both PartTime and FullTime implement iEmployee, you should be able to cast the classes to iEmployee and then call PrintEmployee method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Polymorphism
{
class Program
{
static void Main(string[] args)
{
PartTime Emp1 = new PartTime();
//notice this property is declared in base class but available in derived class.
Emp1.employeeID = 1234;
Emp1.FirstName = "Part Time";
//notice this property is declared in base class but available in derived class.
Emp1.LastName = "Employee";
Emp1.EmployeeType = "Part Time";
iEmployee Emp = Emp1;
Emp.PrintEmployee();
FullTime Emp2 = new FullTime();
//notice this property is declared in base class but available in derived class.
Emp2.employeeID = 5678;
Emp2.FirstName = "Full Time";
//notice this property is declared in base class but available in derived class.
Emp2.LastName = "Employee";
Emp2.EmployeeType = "Full Time";
Emp = Emp2;
Emp.PrintEmployee();
Console.ReadLine();
}
}
}
This is a basic concept but interfaces could be confusing. Hope this helps you understand the concept.
Thank you.
No comments:
Post a Comment