C#
  Home arrow C# arrow Page 6 - Polymorphism in C#
Iron Speed
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Polymorphism in C#
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2007-05-17

    Table of Contents:
  • Polymorphism in C#
  • Versioning with new and override
  • Abstract Classes
  • The Root of All Classes: Object
  • Boxing and Unboxing Types
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    AT&T devCentral & BlackBerry(r) Webcast Series: BlackBerry and GPS -Build Location Awareness into your BlackBerry Applications, July 10th -1:00PM EST. Register Today!

    Polymorphism in C# - Summary


    (Page 6 of 6 )

     

    1. Specialization is described as the is-a relationship; the reverse of specialization is generalization.
    2. 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.
    3. C# implements specialization through inheritance.
    4. 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.
    5. You implement inheritance by adding a colon after the name of the derived class, followed by the name of its base class.
    6. 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.
    7. Classes, like members, can also use the access modifiers public,private, andprotected, though the vast majority of non-nested classes will be public.
    8. 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.
    9. 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.
    10. 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.
    11. Any class marked assealed cannot be derived from.
    12. 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.
    13. 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.
    14. 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 an excerpt from the book "Learning C# 2005, Second Edition,"...
     

    Buy this book now. 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.

    C# ARTICLES

    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...
    - Polymorphism in C#
    - Inheritance in C#
    - C# Events Explained
    - C# Delegates Explained
    - C# StreamReader and StreamWriter Explained
    - C# FileStream Explained

    IBM developerWorks




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway