Check the example of polymorphism below. We have a class called 'SalaryCalculator' with a method called 'Calculate' and a parameter called 'IEmployee.' The 'Calculate' method has no idea what a 'IEmployee' is. Any class that implements the polymorphism 'IEmployee' can be used.
class Program
{
static void Main(string[] args)
{
Salarycalculator salaryCalculator = new Salarycalculator();
salaryCalculator.calculate(new Architect());
}
}
public class Salarycalculator
{
public void calculate(IEmployee employee)
{
employee.CalculateSalary();
}
}
public interface IEmployee
{
void CalculateSalary();
}
public class Developer : IEmployee
{
public void CalculateSalary()
{
}
}
public class Architect : IEmployee
{
public void CalculateSalary()
{
}
}
}
The place receiving the object is where runtime polymorphism works, not the object initialization. 'calculate(IEmployee employee)' is an example where the 'Calculate' method only learns the type of 'IEmployee' at runtime. It can be of type 'Developer' or 'Architect,' which implies the 'Calculate' method will run polymorphically depending on the kind of object.