Chapter 11: Inheritance


Inheritance is one of the three foundational principles of object-oriented programming because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class can then be inherited by other, more specific classes, each adding those things that are unique to it.

In the language of C#, a class that is inherited is called a base class. The class that does the inheriting is called a derived class. Therefore, a derived class is a specialized version of a base class. It inherits all of the variables, methods, properties, operators, and indexers defined by the base class and adds its own unique elements.

Inheritance Basics

C# supports inheritance by allowing one class to incorporate another class into its declaration. This is done by specifying a base class when a derived class is declared. Let’s begin with an example. The following class called TwoDShape defines the attributes of a “generic” two-dimensional shape, such as a square, rectangle, triangle, and so on.

 // A class for two-dimensional objects. class TwoDShape {   public double width;   public double height;   public void showDim() {     Console.WriteLine("Width and height are " +                        width + " and " + height);   } }

TwoDShape can be used as a base class (that is, as a starting point) for classes that describe specific types of two-dimensional objects. For example, the following program uses TwoDShape to derive a class called Triangle. Pay close attention to the way that Triangle is declared.

 // A simple class hierarchy. using System; // A class for two-dimensional objects. class TwoDShape {   public double width;   public double height;   public void showDim() {     Console.WriteLine("Width and height are " +                        width + " and " + height);   } } // Triangle is derived from TwoDShape. class Triangle : TwoDShape {   public string style; // style of triangle   // Return area of triangle.   public double area() {     return width * height / 2;   }   // Display a triangle's style.   public void showStyle() {     Console.WriteLine("Triangle is " + style);   } } class Shapes {   public static void Main() {     Triangle t1 = new Triangle();     Triangle t2 = new Triangle();     t1.width = 4.0;     t1.height = 4.0;     t1.style = "isosceles";     t2.width = 8.0;     t2.height = 12.0;     t2.style = "right";     Console.WriteLine("Info for t1: ");     t1.showStyle();     t1.showDim();     Console.WriteLine("Area is " + t1.area());     Console.WriteLine();     Console.WriteLine("Info for t2: ");     t2.showStyle();     t2.showDim();     Console.WriteLine("Area is " + t2.area());   } }

The output from this program is shown here:

 Info for t1: Triangle is isosceles Width and height are 4 and 4 Area is 8 Info for t2: Triangle is right Width and height are 8 and 12 Area is 48

The Triangle class creates a specific type of TwoDShape, in this case, a triangle. The Triangle class includes all of TwoDShape and adds the field style, the method area( ), and the method showStyle( ). A description of the type of triangle is stored in style, area( ) computes and returns the area of the triangle, and showStyle( ) displays the triangle style.

Notice the syntax that Triangle uses to inherit TwoDShape:

 class Triangle : TwoDShape {

This syntax can be generalized. Whenever one class inherits another, the base class name follows the name of the derived class, separated by a colon. In C#, the syntax for inheriting a class is remarkably simple and easy-to-use.

Because Triangle includes all of the members of its base class, TwoDShape, it can access width and height inside area( ). Also, inside Main( ), objects t1 and t2 can refer to width and height directly, as if they were part of Triangle. Figure 11-1 depicts conceptually how TwoDShape is incorporated into Triangle.

image from book
Figure 11-1: A conceptual depiction of the Triangle class

Even though TwoDShape is a base for Triangle, it is also a completely independent, stand-alone class. Being a base class for a derived class does not mean that the base class cannot be used by itself. For example, the following is perfectly valid:

 TwoDShape shape = new TwoDShape(); shape.width = 10; shape.height = 20; shape.showDim();

Of course, an object of TwoDShape has no knowledge of or access to any classes derived from TwoDShape.

The general form of a class declaration that inherits a base class is shown here:

 class derived-class-name : base-class-name {   // body of class }

You can specify only one base class for any derived class that you create. C# does not support the inheritance of multiple base classes into a single derived class. (This differs from C++, in which you can inherit multiple base classes. Be aware of this when converting C++ code to C#.) You can, however, create a hierarchy of inheritance in which a derived class becomes a base class of another derived class. Of course, no class can be a base class of itself, either directly or indirectly.

A major advantage of inheritance is that once you have created a base class that defines the attributes common to a set of objects, it can be used to create any number of more specific derived classes. Each derived class can precisely tailor its own classification. For example, here is another class derived from TwoDShape that encapsulates rectangles:

 // A derived class of TwoDShape for rectangles. class Rectangle : TwoDShape {   // Return true if the rectangle is square.   public bool isSquare() {     if(width == height) return true;     return false;   }   // Return area of the rectangle.   public double area() {     return width * height;   } }

The Rectangle class includes TwoDShape and adds the methods isSquare( ), which determines if the rectangle is square, and area( ), which computes the area of a rectangle.




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