6.4. Programming for Containment/Delegation

Recall that code reuse comes in two flavors. You have just explored the classical "is-a" relationship. Before you examine the third pillar of OOP (polymorphism), let's examine the "has-a" relationship (also known as the containment/delegation model or aggregation). Assume you have created a new class that models an employee benefits package:

// This new type will function as a contained class.
class BenefitPackage
{
  // Assume we have other members that represent
  // dental/health benefits, and so on.
  public double ComputePayDeduction()
  {
    return 125.0;
  }
}

Obviously, it would be rather odd to establish an "is-a" relationship between the BenefitPackage class and the employee types. (Employee "is-a"

Get Pro C# 2010 and the .NET 4 Platform, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.