Summary

  • Specialization is described as the is-a relationship; the reverse of specialization is generalization.

  • Specialization and generalization are reciprocal and hierarchical—that is, specialization is reciprocal to generalization, and each class can have any number of specialized derived classes but only one parent class that it specializes: thus creating a branching hierarchy.

  • C# implements specialization through inheritance.

  • The inherited class derives the public and protected characteristics and behaviors of the base class, and is free to add or modify its own characteristics and behaviors.

  • You implement inheritance by adding a colon after the name of the derived class, followed by the name of its base class.

  • A derived class can invoke the constructor of its base class by placing a colon after the parameter list and invoking the base class constructor with the keyword base.

  • Classes, like members, can also use the access modifiers public, private, and protected, though the vast majority of nonnested classes will be public.

  • A method marked as virtual in the base class can be overridden by derived classes if the derived classes use the keyword override in their method definition. This is the key to polymorphism: when you call the virtual method on a derived object, the overridden behavior is invoked.

  • A derived class can break the polymorphism of a derived method but must signal that intent with the keyword new. This is unusual and complex, and can be confusing, but it is provided to ...

Get Learning C# 3.0 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.