Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse interface inheritance by using the Implements keyword
DescriptionThis example shows how to use interface inheritance by using the Implements keyword in Visual Basic 6.
Keywordsinterface inheritance, Implements, subclass, derived class
CategoriesSoftware Engineering
 
Visual Basic 6 and earlier versions do not allow true inheritance. However, they allow interface inheritance where one class defines a public interface and then other classes implement it.

In this example, the IVehicle class defines an interface. It is customary for the names of interface classes to start with an I. This class defines a public variable Speed. That will be provided in the implementing classes as property Get and Let procedures.

The interface also defines a public Drive method and a read-only VehicleType property.

 
Option Explicit

' This is a IVehicle interface. It defines public
' properties and methods
' that implementing classes must provide. It does not
' provide any
' implementation for them.

' The current speed.
Public Speed As Single

' Start driving at this speed.
Public Sub Drive(ByVal new_speed As Single)

End Sub

' A read-only property.
Public Property Get VehicleType() As String

End Property
 
It is in implementing an interface that the real art occurs. In this example, the Car and Truck classes implement IVehicle. The VehicleBase class provides a default implementation of IVehicle. Car and Truck both use it to handle their shared features. This is sort of like the code that would be in the parent class if this were true inheritance.

The VehicleBase class uses the Implements keyword to show that is it implementing the IVehicle interface. After you type the Implements statement, the IDE adds the interface to the left dropdown at the top of the code editor. If you click this dropdown, you will see IVehicle listed. If you select it, then the right dropdown shows the methods defined by the interface. If you select one of these, the IDE opens the method's definition including the necessary decoration. For example, it adds the IVehicle_ in front of the method name and gives any needed parameters. Use this method so you don't need to type all that in yourself.

VehicleBase provides simple default versions of the Drive, Speed, and VehicleType methods defined by the IVehicle interface.

 
Option Explicit

' This class provide a default implementation of the
' IVehicle interface.
' Other classes can delegate to an instance of it.
Implements IVehicle

' A default implementation of Speed.
Private m_Speed As Single

' A default implementation of Drive.
Private Sub IVehicle_Drive(ByVal new_speed As Single)
    IVehicle_Speed = new_speed
End Sub

' Return the speed.
Private Property Get IVehicle_Speed() As Single
    IVehicle_Speed = m_Speed
End Property

' Set the speed.
Private Property Let IVehicle_Speed(ByVal RHS As Single)
    m_Speed = RHS
    Debug.Print "Now driving " & m_Speed
End Property

Private Property Get IVehicle_VehicleType() As String
    IVehicle_VehicleType = "VehicleBase"
End Property
 
The following code shows the Truck class. It implements IVehicle. It uses a private VehicleBase variable to provide a default implementation of most of the IVehicle features. For example, if delegates its Drive and Speed methods to this object.

Truck provides a new CargoCapacity property that it implements itself. It also provides its own implementation of VehicleType rather than using the VehicleBase version.

 
Option Explicit

Implements IVehicle

' Delegates most functions to this object.
Private m_VehicleBase As IVehicle

' The Truck class adds CargoCapacity.
Private m_CargoCapacity As Single

' Instantiate the VehicleBase.
Private Sub Class_Initialize()
    Set m_VehicleBase = New VehicleBase
End Sub

Private Sub Class_Terminate()
    Set m_VehicleBase = Nothing
End Sub

Private Sub IVehicle_Drive(ByVal new_speed As Single)
    m_VehicleBase.Drive new_speed
End Sub

Private Property Get IVehicle_Speed() As Single
    IVehicle_Speed = m_VehicleBase.Speed
End Property

Private Property Let IVehicle_Speed(ByVal RHS As Single)
    m_VehicleBase.Speed = RHS
End Property

Private Property Get IVehicle_VehicleType() As String
    IVehicle_VehicleType = "Truck"
End Property

Public Property Get CargoCapacity() As Single
    CargoCapacity = m_CargoCapacity
End Property

Public Property Let CargoCapacity(ByVal new_value As Single)
    m_CargoCapacity = new_value
    Debug.Print "CargoCapacity is " & m_CargoCapacity
End Property
 
The Car class is similar to the Truck class except it doesn't have a CargoCapacity property.

Note that defining a method for an interface makes the method available only when you treat the object as having the type of the interface. For example, suppose you set an IVehicle variable to a new Car object. Then that variable has Drive and Speed members. However, if you create a new Car variable, the interface does not automatically give the variable those methods.

In this example, the Car class adds new Speed property procedures so the program can set a Car's Speed. Notice that the code delegates these procedures to the IVehicle_Speed procedures (which are in turn delegated to m_VehicleBase) so it doesn't need to write more code.

 
' Car provides an extra property procedure for Speed that
' lets the
' program set a Car's Speed in addition to setting its
' Speed as
' a Vehicle. It delegates to IVehicle_Speed so it doesn't
' need
' to duplicate code.
Public Property Get Speed() As Single
    Speed = IVehicle_Speed
End Property

Public Property Let Speed(ByVal new_value As Single)
    IVehicle_Speed = new_value
End Property
 
The following code shows how the main program uses these classes. It creates a Car and sets an IVehicle variable to point to the Car object. It sets the Car's Speed property and then usees the IVehicle's Speed and Drive methods.

Next the code creates a Truck object, sets an IVehicle variable to it, and uses the IVehicle's Speed and Drive methods. It then sets the Truck's CargoCapacity. The IVehicle does not define CargoCapacity so the code cannot set this property using the IVehicle variable.

 
Private Sub Form_Load()
Dim a_car As Car
Dim a_truck As Truck
Dim a_vehicle As IVehicle

    Debug.Print

    ' Make a Car.
    Set a_car = New Car
    Set a_vehicle = a_car
    Debug.Print "Created new " & a_vehicle.VehicleType
    a_car.Speed = 5         ' Car.Speed
    a_vehicle.Speed = 10    ' IVehicle.Speed
    a_vehicle.Drive 15
    Debug.Print ""

    ' Make a truck.
    Set a_truck = New Truck
    Set a_vehicle = a_truck
    Debug.Print "Created new " & a_vehicle.VehicleType
    a_vehicle.Speed = 20
    a_vehicle.Drive 25
    a_truck.CargoCapacity = 30

    Unload Me
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated