Calling an Overridden Function in the Base Class

   


Listing 16.5 demonstrated our ability to call an indexer of a base class from a derived class with the base access construct. You can also call a method or a property of a base class from a derived class in a similar fashion. Listing 16.6 gives an example of how a method of a base class method can be called with the base access construct from a derived class and why it sometimes can be useful.

Listing 16.6 CallingOverriddenMethod.cs
01: using System; 02: 03: class Car 04: { 05:     private string brandName; 06:     private uint odometer; 07: 08:     public Car(string initialBrandName, uint initialOdometer) 09:     { 10:         brandName = initialBrandName; 11:         odometer = initialOdometer; 12:     } 13: 14:     public Car() 15:     { 16:         brandName = "unknown"; 17:         odometer = 0; 18:     } 19: 20:     public virtual void PrintAllInfo() 21:     { 22:         Console.WriteLine("Brand name: " + brandName); 23:         Console.WriteLine("Odometer: " + odometer); 24:     } 25: } 26: 27: class RacingCar : Car 28: { 29:     private string onBoardCameraName; 30: 31:     public RacingCar(string initialBrandName, uint initialOdometer, 32:         string initialCameraName) : base(initialBrandName, initialOdometer) 33:     { 34:         onBoardCameraName = initialCameraName; 35:     } 36:  37:     public RacingCar() : base() 38:     { 39:         onBoardCameraName = "unknown"; 40:     } 41: 42:     public override void PrintAllInfo() 43:     { 44:         base.PrintAllInfo(); 45:         Console.WriteLine("On board camera name: " + onBoardCameraName); 46:     } 47: } 48: 49: class CarTester 50: { 51:     public static void Main() 52:     { 53:         RacingCar yourRacingCar = new RacingCar("Lotus", 2000, "Nikon"); 54:         yourRacingCar.PrintAllInfo(); 55:     } 56: } Brand name: Lotus Odometer: 2000 On board camera name: Nikon 

The RacingCar class (lines 27 47) contains three instance variables the two instance variables it inherits from the Car class (declared in lines 5 and 6) and onBoardCameraName (declared in line 29). (As mentioned earlier, an on-board camera is often used in a professional racing car to film and transmit pictures from the racing car to a television audience).

The PrintAllInfo method simply prints the values of all the instance variables of an object. In the Car class, it prints out the brandName and the odometer variables. In the RacingCar class, PrintAllInfo needs to print the onBoardCameraName as well as brandName and odometer. Because the requirements of PrintAllInfo in RacingCar are different from PrintAllInfo in Car, we need to override PrintAllInfo in the RacingCar class. We now encounter two incentives to use the base access construct to call PrintAllInfo of Car from within PrintAllInfo of RacingCar. First, we somehow need access to the brandName and odometer instance variables from PrintAllInfo in the RacingCar class, but direct access is not possible because they are both declared private. Second, PrintAllInfo of Car is providing part of the functionality needed by PrintAllInfo of RacingCar in that it prints two of the three instance variables that PrintAllInfo of RacingCar needs to print. So, if we can somehow call PrintAllInfo of the Car class from within PrintAllInfo in RacingCar, all that is left to do is to print the onBoardCameraName. This is done in lines 42 46. Line 44 calls PrintAllInfo for RacingCar's base class Car with the keyword base followed by PrintAllInfo. RacingCar's PrintAllInfo method then only needs to include line 45, which prints out the onBoardCameraName.

Syntax Box 16.3 contains the base access syntax for calling a method, property, or indexer of a base class from a derived class.

Syntax Box 16.3 Base Access

 Base_access(method)::=                  base.<Base_class_method_name> (<Argument_list>); Base_access(property)::=                  base.<Base_class_property_name>; Base_access(indexer)::=                  base[<expression1> [, <expression2>, <expression3> graphics/ccc.gif...] ] 

Notes:

  • The <Argument_list> provided for the Base_access(method) must match the formal parameter list (their number and types) of the method called in the base class.

  • The expression list provided for the Base_access(indexer) must match (their number and types) an indexer of the base class.

  • Any of the three base access constructs can only be written inside the block of a constructor, an instance method, or a get or set accessor of a property or indexer.


graphics/16infig02.gif


   


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

Similar book on Amazon

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