Provide multiple inheritance in C#

Sometimes you might want a class to inherits from more than one parent class. For example, you might define a Vehicle class that has vehicle properties such as MaxSpeed, and a Domicile class with house-like properties such as SquareFeet. You might then like to make a HouseBoat or MotorHome class that inherits from both Vehicle and Domicile.

Unfortunately C# does not allow multiple inheritance. A class can inherit from at most one parent class. However, a class can implement any number of interfaces so, instead of true multiple inheritance, you can use interface inheritance. In this example, you can make HouseBoat inherit from Domicile and implement the IVehicle interface.

Even if you make HouseBoat implement IVehicle, there are a couple of odd issues.

First, if IVehicle defines a MaxSpeed property, you still can't access it directly from HouseBoat the way you could if it truly inherited from a Vehicle class.

Second, if you have some code that you would like to put in a Vehicle class, the HouseBoat class cannot inherit it because it only implements IVehicle--it doesn't really inherit from it.

To solve the second problem, this example uses the following Vehicle class. This class implements IVehicle and provides code that other classes can "inherit."

public class Vehicle : IVehicle
{
// Implement IVehicle.MaxSpeed
private int _MaxSpeed;
int IVehicle.MaxSpeed
{
get { return _MaxSpeed; }
set { _MaxSpeed = value; }
}

// Delegate MaxSpeed to IVehicle.MaxSpeed.
public int MaxSpeed
{
get { return (this as IVehicle).MaxSpeed; }
set { (this as IVehicle).MaxSpeed = value; }
}
}

The class starts by implementing IVehicle.MaxSpeed as required by the interface. It then provides a MaxSpeed property that delegates its value to the IVehicle.MaxSpeed property. That makes it easy to call a Vehicle's MaxSpeed property without needing to create an IVehicle variable to refer to it first.

Now the HouseBoat class inherits from Domicile and implements IVehicle as shown in the following code.

public class HouseBoat : Domicile, IVehicle
{
// Inherits the SquareFeet property from Domicile.

// Delegate IVehicle features to a Vehicle object.
private Vehicle _Vehicle = new Vehicle();
int IVehicle.MaxSpeed
{
get { return _Vehicle.MaxSpeed; }
set { _Vehicle.MaxSpeed = value; }
}

// Delegate MaxSpeed to this object as an IVehicle.
public int MaxSpeed
{
get { return (this as IVehicle).MaxSpeed; }
set { (this as IVehicle).MaxSpeed = value; }
}
}

The class automatically gets the SquareFeet property defined by Domicile via inheritance.

The biggest trick here is that the class includes a private Vehicle object and delegates all IVehicle members to that object. In this example, it implements the IVehicle.MaxSpeed property by using the Vehicle object's MaxSpeed property.

The class finishes by creating its own MaxSpeed property that it delegates to its own IVehicle.MaxSpeed property. That lets you use a HouseBoat's MaxSpeed property without needing to make an IVehicle variable referring to it.

The program uses the following code to demonstrate the HouseBoat class.

// Make some objects.
private void Form1_Load(object sender, EventArgs e)
{
// Make a HouseBoat.
HouseBoat boat = new HouseBoat();
boat.SquareFeet = 100;
boat.MaxSpeed = 10;

// Make an IVehicle variable.
IVehicle ivehicle = boat;
ivehicle.MaxSpeed = 15;
}

The code first makes a HouseBoat and sets its SquareFeet and MaxSpeed properties. If then makes an IVehicle variable referring to the same object and sets its MaxSpeed property.

To summarize, the steps for multiple inheritance are:

  • Make the parent class (Domicile)
  • Make the interface (IVehicle)
    • Define the actual interface (IVehicle)
    • Make a class that implements the interface (Vehicle)
    • Make easy access properties (MaxSpeed) that delegate to the interface's properties (IVehicle.MaxSpeed)
  • Make the child class (HouseBoat)
    • Make the child class inherit from the parent class (Domicile)
    • Give the child class a private instance of the interface implementing class (Vehicle)
    • Make the child class implement the interface by delegating members to the private object
    • Make easy access properties (MaxSpeed) that delegate to the interface's properties (IVehicle.MaxSpeed)

Yes this is all a bit roundabout but it gives the appearance of multiple inheritance from the point of view of the code that uses the HouseBoat class. If you modify the Domicile class, HouseBoat automatically inherits the changes. If you change the implementation code in the Vehicle class, HouseBoat picks up those changes, too.

The big catch is that if you modify the IVehicle interface, you need to update the Vehicle class and the HouseBoat class (and any other classes that use the same technique to "inherit" from Vehicle). At least Visual Studio will tell you if you haven't implemented the interface property in those classes.

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.