What is runtime polymorphism or late binding or dynamic binding?
Runtime polymorphism, also known as late binding or dynamic binding, is a concept in object-oriented programming where the actual method implementation to be executed is determined at runtime, based on the actual type of the object. This is achieved through method overriding and is a fundamental feature of inheritance and subclassing.
For method overriding we use 'virtual' keyword in base class and 'overrides' keyword in the derived class while making inheritance of the classes. The compiler would not be aware of the method available for overriding the functionality in derived classes, so the compiler does not know, hence does not throw any error during compile time. Compiler decides at run time which method he needs to call at run time, if the method doesn’t exist then it throws the error.
Let's illustrate runtime polymorphism with a real-time example involving employees and their calculated salaries:
using System;
class Employee
{
public string Name { get; set; }
public Employee(string name)
{
Name = name;
}
public virtual double CalculateSalary()
{
return 0; // Base implementation for generic employee
}
}
class FullTimeEmployee : Employee
{
public double MonthlySalary { get; set; }
public FullTimeEmployee(string name, double monthlySalary)
: base(name)
{
MonthlySalary = monthlySalary;
}
public override double CalculateSalary()
{
return MonthlySalary;
}
}
class PartTimeEmployee : Employee
{
public double HourlyRate { get; set; }
public int HoursWorked { get; set; }
public PartTimeEmployee(string name, double hourlyRate, int hoursWorked)
: base(name)
{
HourlyRate = hourlyRate;
HoursWorked = hoursWorked;
}
public override double CalculateSalary()
{
return HourlyRate * HoursWorked;
}
}
class Program
{
static void Main(string[] args)
{
Employee fullTimeEmployee = new FullTimeEmployee("John Doe", 4000);
Employee partTimeEmployee = new PartTimeEmployee("Jane Smith", 20, 30);
Console.WriteLine($"{fullTimeEmployee.Name}'s Salary: ${fullTimeEmployee.CalculateSalary()}");
Console.WriteLine($"{partTimeEmployee.Name}'s Salary: ${partTimeEmployee.CalculateSalary()}");
}
}
In this example:
-
The 'Employee' class is the base class with a 'virtual' method 'CalculateSalary()'.
- The 'FullTimeEmployee' and 'PartTimeEmployee' classes inherit from 'Employee' and override the 'CalculateSalary()' method to provide their own salary calculation logic.
- In the 'Main' method, instances of both 'FullTimeEmployee' and 'PartTimeEmployee' are created.
- The 'CalculateSalary()' method is called on each instance, and the appropriate overridden method is executed based on the actual object type.
When you run the program, you'll see the calculated salaries for both types of employees:
John Doe's Salary: $4000
Jane Smith's Salary: $600
Despite calling the same method ('CalculateSalary()'), the program adapts its behavior based on the actual type of the object at runtime. This demonstrates runtime polymorphism, where the decision of which method to call is made dynamically based on the object's actual type.