BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 1.1+

C# Inheritance

The eighteenth part of the C# Object-Oriented Programming tutorial begins the discussion of the use of inheritance. This is a powerful object-oriented concept that permits the creation of hierarchical groups of classes that share common functionality.

What is Inheritance?

Inheritance is one of the most important, and most powerful, of the object-oriented programming concepts. Using inheritance, one class can be derived from another. The derived class, also known as the child class or subclass, inherits functionality from the base class, also known as the parent class or superclass. The subclass can add methods and properties or modify the functionality that it has inherited to provide a more specialised version of the base class.

Using inheritance, classes become grouped together in a hierarchical tree structure. The more generalised classes appear at the root of the hierarchy with the more specific classes appearing on the tree's branches. This categorisation of classes into related types is why inheritance is sometimes referred to as generalisation (or generalization).

When using inheritance, all of the base class's methods, properties, events, indexers, operators and some internal variables are all automatically provided to every subclass. As this functionality is automatically available in the child class, there is no need to duplicate code. This reduces maintenance problems, as changes need only to be made in one class, rather than throughout a series of related classes.

Polymorphism

Another key concept of object-oriented programming is polymorphism. This is the ability for an object to change its behaviour according to how it is being used. This is an important part of inheritance and means that a subclass can be used as if it were an instance of its base class. If, for example, you were to create a class with generic functionality for processing the control of vehicles, and you created a more specialised subclass for cars, the Car objects could be passed to methods that expected a Vehicle object. The method would operate on the using the public interface of the Vehicle class. However, the underlying functionality of the Car class would be used. The same routine could be used to process bicycles, buses and other vehicle types.

This type of polymorphism relies heavily on the encapsulation principle. As the method using the Vehicle object is aware only of the Vehicle class's public interface and not its internal workings, it is easy to substitute a Car object because cars are just a special type of vehicle.

Single Inheritance and Multiple Inheritance

There are two types of inheritance used in modern programming languages. C# and other .NET languages use single inheritance. This means that a subclass may only inherit functionality from a single base class.

Multiple inheritance permits a subclass to have two or more superclasses. In this situation the derived class inherits functionality from several base classes. Multiple inheritance is not supported by C# or the .NET framework. However, this does not stop a class from providing many public interfaces as will be seen in a later article.

Demonstrating Inheritance

To demonstrate the use of inheritance with real-world objects, during the rest of this article we will create an example project based around the Vehicle class. This general class will provide methods and properties that all vehicles provide. We will then create subclasses of Vehicle with more specialised functionality.

To begin, create a new console application named "InheritanceDemo". Create a class file named "Vehicle" and add the following code to the new class to provide a Speed property and Accelerate and Decelerate methods to all vehicles:

private int _speed;     // Miles per hour

public int Speed
{
    get { return _speed; }
}

public void Accelerate(int mph)
{
    _speed += mph;
}

public void Decelerate(int mph)
{
    _speed -= mph;
}

The new Vehicle class can be instantiated and tested in its own right. To ensure the class is working correctly, we can modify the Main method of the program to test the methods and the property:

static void Main(string[] args)
{
    Vehicle v = new Vehicle();
    Console.WriteLine("Speed: {0}mph", v.Speed);    // Outputs "Speed 0mph"
    v.Accelerate(25);
    Console.WriteLine("Speed: {0}mph", v.Speed);    // Outputs "Speed 25mph"
    v.Decelerate(15);
    Console.WriteLine("Speed: {0}mph", v.Speed);    // Outputs "Speed 10mph"
}

Creating a Derived Class

Syntax

The syntax for declaring a derived class is similar to that for any other standard class. To specify the base class to inherit from, its name is appended to the subclass declaration, separated by a colon (:) as follows:

class subclass : baseclass
15 March 2008