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.

The MotorVehicle Class

We can now create a class to represent motor vehicles such as cars, buses, etc. As this class is a specialised vehicle that incorporates all of the functionality of the Vehicle class, we can derive the class from Vehicle. Add a new class file to the project and name it "MotorVehicle". Modify the standard class declaration to add the inheritance relationship.

class MotorVehicle : Vehicle
{
}

Although the new class has no code contained within its code block, it already includes the properties and methods defined in its parent class. We can demonstrate this easily by modifying the Main method of the program as follows:

static void Main(string[] args)
{
    MotorVehicle v = new MotorVehicle();
    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"
}

The new main method is essentially the same as the previous example except that an instance of the MotorVehicle class is used rather than a Vehicle object. The output of the program is identical to the earlier code because the functionality used has been inherited without modification.

For later use in this article, create another class file for bicycles named "Bicycle". Bicycles can also accelerate and decelerate so modify the class so that it also inherits its functionality from the Vehicle class. You can test this class with a simple modification to the Main method.

class Bicycle : Vehicle
{
}

Adding Specialised Functionality to a Derived Class

So far, the MotorVehicle and Bicycle classes are essentially identical to the underlying Vehicle class. The value of specialisation provided by inheritance hierarchies is that the derived classes can implement addition functionality. In the case of motor vehicles, fuel is used. We can therefore add properties to identify how many gallons of fuel are present in the vehicle and how large the fuel tank is. We can also create a method to call when the motor vehicle is refuelled. This functionality will be present to motor vehicles but not bicycles.

To add the properties and method, include the following code in the MotorVehicle class. The FuelRemaining property is read-only as it can only be changed via the Refuel method, which returns the number of gallons of fuel added to completely fill the tank.

private float _fuelRemaining;
private float _tankSize;

public float FuelRemaining
{
    get { return _fuelRemaining; }
}

public float TankSize
{
    get { return _tankSize; }
    set { _tankSize = value; }
}

public float Refuel()
{
    float fuelRequired = _tankSize - _fuelRemaining;
    _fuelRemaining = _tankSize;
    return fuelRequired;
}

To give the Bicycle class additional functionality, let's add a method that rings the bell. To indicate that the rider has used the bell we will simply output a string to the console. Add the following method to the Bicycle class:

public void RingBell()
{
    Console.WriteLine("Ring!");
}

The two derived classes now include all of the functionality of the Vehicle class and the addition items added. To demonstrate the use of the two subclasses, modify the Main method of the program as follows:

MotorVehicle car = new MotorVehicle();
car.TankSize = 11;
Console.WriteLine("Fuel: {0}g", car.FuelRemaining); // Outputs "0g"
car.Refuel();
Console.WriteLine("Fuel: {0}g", car.FuelRemaining); // Outputs "11g"

Bicycle bike = new Bicycle();
bike.RingBell();                                     // Outputs "Ring!"
15 March 2008