Inheritance Basics
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.
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 stores the width and height of a twodimensional object, such as a square, rectangle, triangle, and so on.
Example
using System; namespace ConsoleApplication1 { 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.
Example
using System; namespace ConsoleApplication1 { 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 { 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()); Console.ReadLine(); } } }
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 {
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.) In all cases, a derived class inherits all of the members of its base class. This includes instance variables, methods, properties, and indexers.
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:
Example
// 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.