Polymorphism in C# - Summary
(Page 6 of 6 )
- 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, andprotected, though the vast majority of non-nested classes will be public.
- A method marked asvirtualin the base class can be overridden by derived classes if the derived classes use the keywordoverridein 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 keywordnew. 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 asabstracthas 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 assealed cannot be derived from.
- In C#, all classes (and built-in types) are ultimately derived from theObject class, implicitly, and thus inherit a number of useful methods such asToString.
- 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.
Quiz
Question 11-1. What is the relationship between specialization and generalization?
Question 11-2. How is specialization implemented in C#?
Question 11-3. What is the syntax for inheritance in C#?
Question 11-4. How do you implement polymorphism?
Question 11-5. What are the two meanings of the keywordnew?
Question 11-6. How do you call a base class constructor from a derived class?
Question 11-7. What is the difference between public, protected, and private?
Question 11-8. What is an abstract method?
Question 11-9. What is a sealed class?
Question 11-10. What is the base class of Int32?
Question 11-11. What is the base class of any class you create if you do not otherwise indicate a base class?
Question 11-12. What is boxing?
Question 11-13. What is unboxing?
Exercises
Exercise 11-1. Create a base class, Telephone, and derive a class ElectronicPhonefrom it. InTelephone, create a protected string memberphonetype, and a public methodRing()that outputs a text message like this: “Ringing the <phonetype>.” InElectronicPhone, the constructor should set thephonetypeto “Digital.” In theRun()method, callRing() on theElectronicPhoneto test the inheritance.
Exercise 11-2. Extend Exercise 11-1 to illustrate a polymorphic method. Have the derived class override theRing()method to display a different message.
Exercise 11-3. Change theTelephoneclass to abstract, and makeRing()an abstract method. Derive two new classes fromTelephone:DigitalPhoneandTalkingPhone. Each derived class should set thephonetype, and override theRing()method.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter 11 of Learning C# 2005, Second Edition, written by Jesse Liberty and Brian MacDonald (O'Reilly, 2006; ISBN: 0596102097). Check it out today at your favorite bookstore. Buy this book now.
|
|