<< Chapter < Page | Chapter >> Page > |
Click here to download a zip file containing my version of the program. Extract the folder named Polymorph03 from the zip file and save it somewhere on your disk. Start Visual C# 2010 Express and select Open Project... from the File menu. Navigate to the project folder and select the file with the extension of .sln . This should cause the project to open and be ready to run or debug as described in the earlier module titled Getting Started .
Polymorphism manifests itself in C# in the form of multiple methods having the same name.
From a practical programming viewpoint, polymorphism manifests itself in three distinct forms in C#:
This module discusses method overriding through class inheritance.
With runtime polymorphism based on method overriding, the decision regarding which version of a method will be executed is based on the actual type of objectwhose reference is stored in a reference variable, and not on the type of the reference variable on which the method is called.
The decision regarding which version of the method to call cannot be made at compile time. That decision must be deferred and made at runtime. This issometimes referred to as late binding.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
A complete listing of the program discussed in this module is provided in Listing 7 .
Listing 7 . Project Polymorph03.
/*Project Polymorph03
Copyright 2009, R.G.BaldwinThis program illustrates downcasting
and polymorphic behaviorProgram output is:m in class B
m in class Bm in class A
*********************************************************/using System;
class A {public virtual void m() {
Console.WriteLine("m in class A");}//end method m()
}//end class A//======================================================//
class B : A {public override void m() {
Console.WriteLine("m in class B");}//end method m()
}//end class B//======================================================//
public class Polymorph03 {public static void Main() {
Object var = new B();//Following will compile and run
((B)var).m();//Following will also compile
// and run due to polymorphic// behavior.
((A)var).m();//Following will not compile
//var.m();//Instantiate obj of class A
var = new A();//Call the method on it
((A)var).m();// Pause until the user presses any key.
Console.ReadKey();}//end Main
}//end class Polymorph03
-end-
Notification Switch
Would you like to follow the 'Xna game studio' conversation and receive update notifications?