O'Reilly logo

Pro C# 2010 and the .NET 4 Platform, Fifth Edition by Andrew Troelsen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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"

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required