Method Overriding and Overloading Are Different Mechanisms

   


It is important to distinguish between method overloading presented in Chapter 12, "Class Anatomy Part I: static Class Members and Method Adventures," and method overriding presented in this chapter. As noted in Syntax Box 16.2, an overriding method must have the same name and the same set of formal parameter types as the virtual base class method. In contrast, an overloading method has the same name but a different set of formal parameter types.

So if you included a method in your derived class that had the same name as a method in the base class but different formal parameter types, the method of the base class would not be overridden by the derived class method because of the differing number of formal parameters. The derived class would inherit this base class method instead.

It is important to notice, though, that this inherited base class method is not treated exactly like a method defined in the derived class. Consider a scenario where a base class and its derived class both contain methods with the same name (let's call it MyMethod) but different formal parameters. If MyMethod is called through an object of the derived class, the compiler will first try to match the arguments provided with this method call to any of the MyMethod definitions written in the derived class and, at this stage, ignore the MyMethod methods inherited from the base class. It will even perform implicit conversions if necessary to find a match before it considers the MyMethod methods inherited from the base class. Only if this also fails will it consider the methods inherited from the base class. If a chain of derived classes exists containing MyMethod methods with different formal parameters, then the compiler will move up through the hierarchy from the class for which the method was called. On each level, it will try to find a match in a similar fashion to that described for the class for which it was called originally.

Listing 16.9 illustrates this scenario. It contains an Animal class with two methods called Move. The Dog class is derived from the Animal class and also contains two methods called Move. Both of the Move methods in the Animal class have different formal parameters from the Move methods in the Dog class. They are not overridden but inherited (for that reason we don't need to include the override keyword when defining the two Move methods in the Dog class).

Listing 16.9 OverridingOverloading.cs
01: using System; 02: 03: public class Animal 04: { 05:     public virtual void Move(short distance) 06:     { 07:         Console.WriteLine("Animal.Move(short). Distance: { 0} ", distance); 08:     } 09: 10:     public virtual void Move(double distance, string direction) 11:     { 12:         Console.WriteLine("Animal.Move(double, string). Distance:{0}  Direction: { 1}  graphics/ccc.gif", 13:             distance, direction); 14:     } 15: } 16: 17: public class Dog : Animal 18: { 19:     public void Move(int distance) 20:     { 21:         Console.WriteLine("Dog.Move(int). Distance: { 0} ", distance); 22:     } 23: 24:     public void Move(byte distance) 25:     { 26:         Console.WriteLine("Dog.Move(byte). Distance: { 0} ", distance); 27:     } 28: } 29: 30: class Tester 31: { 32:     public static void Main() 33:     { 34:         Dog fido = new Dog(); 35: 36:         int myInt = 45; 37:         short myShort = 25; 38:         double myDouble = 5.6; 39: 40:         fido.Move(myInt); 41:         fido.Move(myDouble, "North"); 42:         fido.Move(myShort); 43:     } 44: } Dog.Move(int). Distance: 45 Animal.Move(double, string). Distance: 5.6 Direction: North Dog.Move(int). Distance: 25 

Not surprisingly, the call in line 40 calls the Move(int) method of the Dog class.

The compiler cannot find any Move parameter lists among the Move methods defined in the Dog class that matches this argument combination (double, string) provided with the call to Move in line 41. Consequently, it moves its focus to the Move methods inherited from the Animal class where it finds the matching Move(double..., string...) method.

Line 42 reveals that the compiler will use implicit conversion to find a matching parameter list before it will consider methods inherited from a base class. Even though the single argument of type short has a perfect match in the inherited method Move(short...) (defined in lines 5 8), the compiler uses implicit conversion to choose the less perfect match in Move(int...) (defined in lines 19 22).


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

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