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 allow for versioning of derived classes. Typically, you will use the keyword override (rather than new) to indicate that you are modifying the behavior of the base class’s method.

  • A method marked as abstract has no implementation—instead, it provides a virtual method name and signature that all derived classes must override. Any class with an abstract method is an abstract class, and cannot be instantiated.

  • Any class marked as sealed cannot be derived from.

  • In C#, all classes (and built-in types) are ultimately derived from the Object class implicitly, and thus inherit a number of useful methods, such as ToString.

The topics in this chapter were a bit more complex than anything we’ve discussed up to this point, but they allow you to see the power and scope of C# in particular and object-oriented languages in general. We think it’s pretty impressive that every object built into C# derives from just one class (Object), and once you grasp that, you can see how you might harness that power to create your own derived classes.

You also saw a lot of overloading in this chapter, and you can see how derived classes can build in their parents’ method implementations to create new and different methods. In the next chapter, you’ll take that to the extreme, and see that you can even override simple operators, such as + and -, in almost the same way as you did with methods.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset