What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleProvide multiple inheritance in Visual Basic .NET
DescriptionProvide multiple inheritance in Visual Basic .NET,
Keywordsinheritance, interfaces, multiple inheritance, example, example program, Windows Forms programming, Visual Basic .NET, VB.NET
CategoriesAlgorithms, Software Engineering
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
    Implements IVehicle

    Private _MaxSpeed As Integer
    Public Property MaxSpeed() As Integer Implements _
            Return _MaxSpeed
        End Get
        Set(ByVal value As Integer)
            _MaxSpeed = value
        End Set
    End Property
End Class
The class simply implements the MaxSpeed property required by the interface.

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

Public Class HouseBoat
    Inherits Domicile
    Implements IVehicle

    ' Inherits the SquareFeet property from Domicile.

    ' Delegate IVehicle features to a Vehicle object.
    Private _Vehicle As New Vehicle()
    Public Property MaxSpeed() As Integer Implements _
            Return _Vehicle.MaxSpeed
        End Get
        Set(ByVal value As Integer)
            _Vehicle.MaxSpeed = value
        End Set
    End Property
End Class
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 program uses the following code to demonstrate the HouseBoat class.

' Make some objects.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    ' Make a HouseBoat.
    Dim house_boat As New HouseBoat()
    house_boat.SquareFeet = 100
    house_boat.MaxSpeed = 10

    ' Make an IVehicle variable.
    Dim i_vehicle As IVehicle = house_boat
    i_vehicle.MaxSpeed = 15
End Sub
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 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
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.