Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that empowers a class to manifest multiple forms. It stands as one of the central tenets of OOP. This characteristic allows us to invoke methods and utilize derived classes through a reference variable of the base class during runtime. To achieve this, we use the "virtual" keyword to declare a method as virtual in the base class, and in the derived class, we employ the "override" keyword to provide a customized implementation of the same method.
using System
class Employee{ // parent class
string FirstName;
string LastName;
string email;
public virtual void PrintName(){ //base class
Console.WriteLine(FirstName + " " + LastName)
}
public class FulltimeEmployee : Employee // derived class
{
float YearlySalary;
public override void PrintName(){
Console.WriteLine(FirstName + " " + LastName + " " + "Fulltime Employee" )
}
}
public class ParttimeEmployee: Employee
{
float HourlyRate;
public override void PrintName(){
Console.WriteLine(FirstName + " " + LastName + " " + "Parttime Employee" )
}
}
public class TempEmployee: Employee
{
float HourlyRate;
public override void PrintName(){
Console.WriteLine(FirstName + " " + LastName + " " + "Temporay Employee" )
}
}
class Main {
public static void Main (){
Employee[] employees = new Employee[4];
employees[0] = new Employee();
employees[1] = new FulltimeEmployee();
employee[2] = new ParttimeEmployee();
employee[3] = new TempEmployee()
foreach (Employee e in employees){
e.PrintName()
}
}
}