Working with Polymorphism

Using virtual methods and the override keyword, you can implement polymorphism , the capability to determine at runtime which type of object is stored in a variable and to call that object's methods correctly. For example, if you derived Menu from a base class named Window and each of these classes had a different virtual version of the Open method, you could put off until runtime which type of object, Window or Menu , to place in a Window variable. If you put a Window object into a Window variable and called its Open method, Window.Open would be called, but if you placed a Menu object into a Window variable (which is legal since Menu is derived from Window ) and called its Open method, Menu.Open would be called.

In other words, you don't have to specify until runtime which type of object you want to work with, which allows you to use the same code to handle objects of many different types. For example, you might have code in a procedure that you can pass Window objects to. Because of runtime polymorphism, you can also pass Menu objects to that procedure and the same code will work fine (as long as you don't use those objects to call methods defined only in Menu and not in Window ).

For example, say you have the following version of the Window class. Note that the Open method is declared virtual:

 
 public class Window {   private string openingMessage = "Opening...";  public virtual void Open()  {     System.Console.WriteLine(openingMessage);   } } 

The Menu class is derived from Window , and it specifically overrides Open to display a different message than Window.Open :

 
 public class Menu : Window {   private string openingMessage = "Displaying items...";  public override void Open()   {   System.Console.WriteLine(openingMessage);   }  } 

Now you can place objects of either the Window or Menu classes into a Window variable, windowVariable . When you load a Window object into this variable and call windowVariable.Open , the Window version of Open will be called, but when you load a Menu object into this variable and call windowVariable.Open , the Menu version of Open will be called. In other words, you can use the same code with different types of objects. You can see this at work in ch04_06.cs, Listing 4.6. (Note that if you hadn't declared Open virtual in Window and overridden it in Menu , windowVariable.Open would call Window.Open no matter whether you had a Window or Menu object in windowVariable .)

Listing 4.6 Using Polymorphism (ch04_06.cs)
 public class ch04_06 {   static void Main()   {  Window windowVariable;   windowVariable = new Window();   windowVariable.Open();   windowVariable = new Menu();   windowVariable.Open();  } } public class Window {   private string openingMessage = "Opening...";   public virtual void Open()   {     System.Console.WriteLine(openingMessage);   } } public class Menu : Window {   private string openingMessage = "Displaying items...";   public override void Open()   {     System.Console.WriteLine(openingMessage);   } } 

Here's what you see when you run ch04_06.cs. Note that although we made the same call, windowVariable.Open , the first time Window.Open was called and the second time Menu.Open was called:

 
 C:\>ch04_06 Opening... Displaying items... 


Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

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