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.

Overriding Base Class Functionality

Inheritance is not limited to additional functionality. It is common for derived classes to modify the behaviour of superclass methods and properties according to the use of the subclass. In our example, all vehicles should provide the ability to indicate when they are planning to turn left or right. The method of indication will vary according to the type of vehicle.

When a method in a base class is designed to be overridden by its subclasses, the method is marked as such using the virtual keyword. In the following virtual method, the basic Vehicle class will simply output a message describing the indication direction given. This will be overridden in the MotorVehicle and Bicycle classes.

Add the following method to the Vehicle class:

public virtual void Indicate(bool turningLeft)
{
    if (turningLeft)
        Console.WriteLine("Turning left");
    else
        Console.WriteLine("Turning right");
}

In the MotorVehicle class, the basic outputted message will be overridden. This is achieved by creating a method with the same scope, name and parameters, or signature, as the base class method, and prefixing the method's return type with the override keyword.

Add the following method to the MotorVehicle class:

public override void Indicate(bool turningLeft)
{
    if (turningLeft)
        Console.WriteLine("Flashing left indicator");
    else
        Console.WriteLine("Flashing right indicator");
}

For bicycles, the rider will raise their arm to indicate that they are turning so add the following code to the Bicycle class:

public override void Indicate(bool turningLeft)
{
    if (turningLeft)
        Console.WriteLine("Raising left arm");
    else
        Console.WriteLine("Raising right arm");
}

The two subclasses now share the same Indicate method as part of their public interface. However, the internal functionality of the two methods is different as can be seen by executing the following code:

MotorVehicle car = new MotorVehicle();
car.Indicate(true);                     // Outputs "Flashing left indicator"

Bicycle bike = new Bicycle();
bike.Indicate(true);                    // Outputs "Raising left arm"

Calling Base Class Functionality

In the previous example, a method from the base class was overridden by a derived method with a matching signature. Often the functionality from parent class is required but needs to be extended by the child class. By using the special base object, a subclass can access base class's functions. Calling the base class method from within an overridden member can be used to combine functionality. In fact, a subclass can access any of the base class members via the base object.

This can be demonstrated by modifying the Indicate method of the Bicycle class. In the following example, the base class method is called first before the subclass functionality executes.

public override void Indicate(bool turningLeft)
{
    base.Indicate(turningLeft);

    if (turningLeft)
        Console.WriteLine("Raising left arm");
    else
        Console.WriteLine("Raising right arm");
}

Calling the modified method shows the results of both operations:

Bicycle bike = new Bicycle();
bike.Indicate(true);

/* OUTPUT

Turning left
Raising left arm

*/
15 March 2008