We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace EmployeeDynamicPoly {
// Abstract base class for all employees
public abstract class Employee { public string Name { get; set; } public int ID { get; set; }
public Employee(string name, int id)
{ Name = name; ID = id; }
// Abstract method - must be implemented by derived classes
// This is where polymorphism begins public abstract double CalculateMonthlySalary(); // Virtual method - can be overridden by derived classes public virtual void DisplayInfo() { Console.WriteLine($"Employee ID: {ID}, Name: {Name}"); } }
// Derived class for full-time employees
public class FullTimeEmployee : Employee { public double AnnualSalary { get; set; }
public FullTimeEmployee(string name, int id, double annualSalary) : base(name, id)
{ AnnualSalary = annualSalary; }
// Override of the abstract method
// This is a polymorphic implementation public override double CalculateMonthlySalary() { return AnnualSalary / 12; }
// Override of the virtual method
// Another example of polymorphism public override void DisplayInfo() { base.DisplayInfo(); Console.WriteLine($"Type: Full-Time, Annual Salary: ${AnnualSalary:F2}"); } }
// Derived class for part-time employees
public class PartTimeEmployee : Employee { public double HourlyRate { get; set; } public int HoursWorked { get; set; }
public PartTimeEmployee(string name, int id, double hourlyRate, int hoursWorked) :
// Yet another polymorphic implementation of CalculateMonthlySalary
public override double CalculateMonthlySalary() { return ContractAmount / ContractDurationMonths; }
// Yet another polymorphic implementation of DisplayInfo
public override void DisplayInfo() { base.DisplayInfo(); Console.WriteLine($"Type: Contract, Contract Amount: ${ContractAmount:F2}, Duration: {ContractDurationMonths} months"); } } class Program { static void Main(string[] args) { // Create an array of Employee objects // This is where we set up for runtime polymorphism Employee[] employees = new Employee[3];
// Populate the array with different types of employees
employees[0] = new FullTimeEmployee("John Doe", 1001, 60000); employees[1] = new PartTimeEmployee("Jane Smith", 1002, 20, 80); employees[2] = new ContractEmployee("Bob Johnson", 1003, 30000, 6);
// Iterate through the employees array
foreach (Employee emp in employees) { // Here's where runtime polymorphism occurs: // The correct version of DisplayInfo and CalculateMonthlySalary // is called based on the actual type of the object, not the reference type emp.DisplayInfo(); Console.WriteLine($"Monthly Salary: ${emp.CalculateMonthlySalary():F2}"); Console.WriteLine(); } } } }
demonstrates run-time polymorphism in several ways:
1. The Employee class is abstract and defines an abstract method
CalculateMonthlySalary(). This forces all derived classes to implement this method, but allows each to do so in its own way. 2. The DisplayInfo() method is virtual in the base class and overridden in each derived class. This allows each type of employee to display its information differently. 3. In the Main method, we create an array of Employee objects, but we populate it with different derived types (FullTimeEmployee, PartTimeEmployee, ContractEmployee). 4. When we iterate through this array and call DisplayInfo() and CalculateMonthlySalary() on each object, the program determines at runtime which version of these methods to call based on the actual type of the object, not the type of the reference (which is Employee). This ability to use derived class objects through a base class reference, with the correct method implementations being called at runtime, is the essence of run-time polymorphism.