Section 11.8. Summary


11.8. Summary

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

  • Specialization and generalization are reciprocal and hierarchicalthat 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 non-nested 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 in which you have a collection of references to a base class but each object is actually an instance of a derived class. When you call the virtual method on each 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, complex and can be confusing, but is provided to allow for versioning of derived classes. Typically, you will use the keyword overrides (rather than new) to indicate that you are modifying the behavior of the base class's method.

  • A method marked as abstract has no implementationinstead, 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 .

  • When you pass a value type to a method or collection that expects a reference type, the value type is "boxed" and must be explicitly "unboxed" when retrieved.

  • Generics make boxing and unboxing less common, and well-designed code will have little or no boxing or unboxing.



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net