Microsoft Visual C#.NET 2003 Kick Start

Overriding Virtual Methods

In this chapter's first example, we defined the Open method in the derived Menu class with the new keyword:

public new void Open() { System.Console.WriteLine(openingMessage); }

The new keyword hides an inherited member with the same signature, and you use it when you are redefining a base class method into something entirely new. But here, Open isn't entirely new. In this case, we're just customizing it for the Menu class. Logically, this method does the same thing for the Menu class as it did for the Window class. For that reason, we should really be overriding the Open method, not specifying that we're redefining it entirely. Overriding it replaces it much as new does, but if you override a method, you're indicating that you're really just customizing it for the current class.

To override a method in a base class with the same signature, that base class method must be declared virtual or abstract . We'll take a look at virtual methods here and abstract methods in the next section. To make a method virtual, you just use the virtual keyword, as we can do with the Open method in the Window base class:

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

Methods are declared virtual so that they can be overridden with the override keyword, and you can see how to override the Open method with the new version in ch04_04.cs, Listing 4.4.

Listing 4.4 Overriding Virtual Methods (ch04_04.cs)

public class ch04_04 { public static void Main() { Window window = new Window(); window.Open(); Menu menu = new Menu(); menu.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 this codejust what you saw when we used the new keyword, but here we're using the virtual and override keywords to override a virtual method:

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

So what's the difference between new and override ? You use new when you're replacing a base class method with the same signature, and you use override when you're customizing it in the current class.

Note that you cannot override a non-virtual or static methodthe method you're overriding in C# must be virtual, abstract, or itself be an overriding version of a base class's method (technically there actually are some exceptions to this rule; sometimes you can override methods in other cases if you're working directly in MSIL, for example). In Java, all methods are virtual, but in C++ and C#, they're not virtual unless you use the virtual keyword. (What happens if a base class and a derived class both have the same method marked as virtual? C# will use the version in the base class, and warn you about the version in the derived class.)

FOR C++ PROGRAMMERS

Overriding methods in C# requires the override keyword.

Категории