Creating a Multilevel Hierarchy


Up to this point, we have been using simple class hierarchies consisting of only a base class and a derived class. However, you can build hierarchies that contain as many layers of inheritance as you like. As mentioned, it is perfectly acceptable to use a derived class as a base class of another. For example, given three classes called A, B, and C, C can be derived from B, which can be derived from A. When this type of situation occurs, each derived class inherits all of the traits found in all of its base classes. In this case, C inherits all aspects of B and A.

To see how a multilevel hierarchy can be useful, consider the following program. In it, the derived class Triangle is used as a base class to create the derived class called ColorTriangle. ColorTriangle inherits all of the traits of Triangle and TwoDShape, and adds a field called color, which holds the color of the triangle.

 // A multilevel hierarchy. using System; class TwoDShape {   double pri_width;  // private   double pri_height; // private   // Default constructor.   public TwoDShape() {     width = height = 0.0;   }   // Constructor for TwoDShape.   public TwoDShape(double w, double h) {     width = w;     height = h;   }   // Construct object with equal width and height.   public TwoDShape(double x) {     width = height = x;   }   // Properties for width and height.   public double width {      get { return pri_width; }      set { pri_width = value; }   }   public double height {      get { return pri_height; }      set { pri_height = value; }   }   public void showDim() {     Console.WriteLine("Width and height are " +                        width + " and " + height);   } } // A derived class of TwoDShape for triangles. class Triangle : TwoDShape {   string style; // private   /* A default constructor. This invokes the default      constructor of TwoDShape. */   public Triangle() {     style = "null";   }   // Constructor   public Triangle(string s, double w, double h) : base(w, h) {     style = s;   }   // Construct an isosceles triangle.   public Triangle(double x) : base(x) {     style = "isosceles";   }   // Return area of triangle.   public double area() {     return width * height / 2;   }   // Display a triangle's style.   public void showStyle() {     Console.WriteLine("Triangle is " + style);   } } // Extend Triangle. class ColorTriangle : Triangle {   string color;   public ColorTriangle(string c, string s,                        double w, double h) : base(s, w, h) {     color = c;   }   // Display the color.   public void showColor() {     Console.WriteLine("Color is " + color);   } } class Shapes6 {   public static void Main() {     ColorTriangle t1 =          new ColorTriangle("Blue", "right", 8.0, 12.0);     ColorTriangle t2 =          new ColorTriangle("Red", "isosceles", 2.0, 2.0);     Console.WriteLine("Info for t1: ");     t1.showStyle();     t1.showDim();     t1.showColor();     Console.WriteLine("Area is " + t1.area());     Console.WriteLine();     Console.WriteLine("Info for t2: ");     t2.showStyle();     t2.showDim();     t2.showColor();     Console.WriteLine("Area is " + t2.area());   } }

The output of this program is shown here:

 Info for t1: Triangle is right Width and height are 8 and 12 Color is Blue Area is 48 Info for t2: Triangle is isosceles Width and height are 2 and 2 Color is Red Area is 2

Because of inheritance, ColorTriangle can make use of the previously defined classes of Triangle and TwoDShape, adding only the extra information it needs for its own, specific application. This is part of the value of inheritance; it allows the reuse of code.

This example illustrates one other important point: base always refers to the constructor in the closest base class. The base in ColorTriangle calls the constructor in Triangle. The base in Triangle calls the constructor in TwoDShape. In a class hierarchy, if a base class constructor requires parameters, then all derived classes must pass those parameters “up the line.” This is true whether or not a derived class needs parameters of its own.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

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