Multiple Levels of Derived Classes

   


It is possible to use a derived class as a base class for another derived class. In other words, you can create as many levels of derived classes as you want. For example, you could create a general TransportationVehicle class in line with Figure 16.1 and, from this class, derive a SurfaceVehicle class. This again could be a base class for a Car class that finally could become the base class for our well-known SportsCar, FamilyCar, and RacingCar classes, as illustrated in Figure 16.9.

Figure 16.9. Multiple levels of inheritance.
graphics/16fig09.gif

Listing 16.8 demonstrates how a part of Figure 16.9 can be implemented by implementing the three classes SurfaceVehicle, Car, and FamilyCar and shows that class members are inherited across several levels.

Listing 16.8 ThreeInheritanceLevels.cs
 01: using System;  02:  03: class SurfaceVehicle  04: {  05:     private float weight;  06:  07:     public SurfaceVehicle()  08:     {  09:         weight = 0;  10:     }  11:  12:     public SurfaceVehicle(float initialWeight)  13:     {  14:         Console.WriteLine("Now initializing weight");  15:         weight = initialWeight;  16:     }  17:  18:     public float Weight  19:     {  20:         get  21:         {  22:             return weight;  23:         }  24:  25:         set  26:         {  27:             weight = value;  28:         }  29:     }  30: }  31:  32: class Car : SurfaceVehicle  33: {  34:     private uint odometer;  35:  36:     public Car() : base ()  37:     {  38:         odometer = 0;  39:     }  40:   41:     public Car(uint initialOdometer, float initialWeight): base(initialWeight)  42:     {  43:         Console.WriteLine("Now initializing odometer");  44:         odometer = initialOdometer;  45:     }  46:  47:     public uint Odometer  48:     {  49:         get  50:         {  51:             return odometer;  52:         }  53:  54:         set  55:         {  56:             odometer = value;  57:         }  58:     }  59: }  60:  61: class FamilyCar : Car  62: {  63:     private byte numberOfBabySeats;  64:  65:     public FamilyCar() : base()  66:     {  67:         numberOfBabySeats = 0;  68:     }  69:  70:     public FamilyCar(byte initialNumberOfBabySeats, uint initialOdometer,  71:         float initialWeight) : base (initialOdometer, initialWeight)  72:     {  73:         Console.WriteLine("Now initializing numberOfBabySeats");  74:         numberOfBabySeats = initialNumberOfBabySeats;  75:     }  76:  77:     public byte NumberOfBabySeats  78:     {  79:         get  80:         {  81:             return numberOfBabySeats;  82:         }  83:  84:         set  85:         {  86:             numberOfBabySeats = value;  87:         }  88:     }  89: }  90:  91: class Tester  92: {  93:     public static void Main()  94:     {  95:         FamilyCar myCar = new FamilyCar(1,10000,1500);  96:  97:         Console.WriteLine("\ nWeight: { 0}\ nOdometer: { 1}\ nBaby Seats: { 2}\ n",  98:             myCar.Weight, myCar.Odometer, myCar.NumberOfBabySeats);  99:         myCar.Weight = 1800; 100:         myCar.Odometer = 4000; 101:         myCar.NumberOfBabySeats = 2; 102:         Console.WriteLine("Weight: { 0}\ nOdometer: { 1}\ nBaby Seats: { 2} ", 103:             myCar.Weight, myCar.Odometer, myCar.NumberOfBabySeats); 104:     } 105: } Now initializing weight Now initializing odometer Now initializing numberOfBabySeats Weight: 1500 Odometer: 10000 Baby Seats: 1 Weight: 1800 Odometer: 4000 Baby Seats: 2 

Each of the classes in Listing 16.8 declares one instance variable and one associated property. The Car class is derived from the SurfaceVehicle class, and, consequently, inherits the instance variable weight and the property Weight. The FamilyCar is derived from the Car class and not only inherits the odometer instance variable and Odometer property declared in Car, but also the two class members that Car inherited from SurfaceVehicle. This is demonstrated in the Main method of the Tester class, where myCar (lines 99 103) is able to use the three properties Weight (and therefore, also the instance variable weight), Odometer, and NumberOfBabySeats.

Both the Car and FamilyCar class constructors apply constructor initializers to call a constructor of their base class (see lines 36, 41, 65, and 71). For example, when FamilyCar's constructor (lines 70 75) is called, it will, as a first step, call the Car constructor (lines 41 45). When called, the Car constructor will, as a first step, call the SurfaceVehicle constructor (lines 12 16), which will have its statements executed, followed by those of the Car class constructor, followed finally by those of the FamilyCar class constructor. I have inserted the print statements in lines 14, 43, and 73 for demonstration purposes only, so you can follow the series of events just described. The first three lines of the sample output are a result of these print statements and are initiated by the creation of a FamilyCar object in line 95, which makes a call to the FamilyCar constructor in lines 70 75.

Notice how efficiently the three classes reuse the code in this program. For example, not only are Weight and weight reused in Car, but also in FamilyCar. In general, and if suitable, large hierarchies of classes result in large amounts of reused code.

Ancestor and Descendant Classes

graphics/common.gif

Sometimes a derived class is referred to as a child class, and a base class is referred to as a parent class. This logic is extended to chains of derived classes where a base class of a base class (and so on) is called an ancestor class, and a derived class of a derived class (and so on) is called a descendant class.


Note

graphics/common.gif

You can use the base access construct (defined in Syntax Box 16.3) to call a method, property, or indexer in a base class, but there is no construct available to call an ancestor further up the chain of base classes. For example, the following kind of syntax is invalid:

 base.base.<Ancestor_method_name>; 



   


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