Polymorphic Payroll
An abstract base class lets payroll code treat every employee uniformly.
How it works
Calculate pay for different employee types.
Runtime dispatch calls the correct Pay implementation for each derived employee.
Runnable C# example
Program.cs
using System;
using System.Linq;
abstract class Employee { public abstract decimal Pay(); }
class Salaried(decimal salary) : Employee { public override decimal Pay() => salary; }
class Hourly(decimal rate, int hours) : Employee { public override decimal Pay() => rate * hours; }
class Program
{
static void Main()
{
Employee[] staff = { new Salaried(1000), new Hourly(20, 10) };
Console.WriteLine(staff.Sum(employee => employee.Pay()));
}
}
1200Practice with the debugger
Set a breakpoint on an important assignment or condition, click Debug, and inspect the local values before stepping to the next line.