Mastering Interface Inheritance in Visual Basic 6.0

Interface Inheritance in Visual Basic 6.0

A comprehensive guide to implementing polymorphism, structural contracts, and reuse patterns in classic COM-based object environments.

1. Introduction to VB6 OOP Limitations

If you come from languages like C++, Java, or C#, you are likely accustomed to Implementation Inheritance (using terms like extends, inherits, or base classes). This model allows a child class to inherit fields and logic from a parent class directly.

Visual Basic 6.0 does not support Implementation Inheritance.

Because VB6 is built entirely on Microsoft's Component Object Model (COM), it relies strictly on interfaces to define type relations. Instead of inheritance, VB6 achieves object relationship hierarchies through Interface Inheritance and Composition.

Why COM Excluded Implementation Inheritance
COM was designed to solve binary compatibility issues between compiled library binaries across compilers. Implementation inheritance introduces the "fragile base class problem," where changes to a base library break compiled subclasses. By forcing binary communication to occur strictly via immutable interfaces, COM avoids this structural vulnerability.

2. The Implements Keyword

In VB6, interface inheritance is achieved using the Implements statement. When a class utilizes Implements, it signs a contract guaranteeing that it will define every subroutine, function, and property declared in the target interface.

This allows you to write polymorphic code: code that interacts with different classes through a single shared interface wrapper.

IAnimal (Interface)
Contracts only
← Implements
CDog (Class)
Custom Bark Logic
CCat (Class)
Custom Meow Logic

3. Step 1: Defining the Interface Blueprint

To create an interface, you simply add a standard Class Module to your VB6 project. By convention, interface classes are prefixed with an "I" (e.g., IAnimal).

Inside the interface class, you define the methods and properties. However, you write **no logic** inside the procedures; they remain completely empty.

File: IAnimal.cls

Option Explicit

' --- IAnimal Interface Module ---

' Read-only Property contract
Public Property Get Species() As String
    ' Left completely blank
End Property

' Subroutine contract
Public Sub MakeSound()
    ' Left completely blank
End Sub

4. Step 2: Implementing the Interface

Once you have defined your interface class, you implement it in a concrete class module (e.g., CDog.cls).

When you add Implements IAnimal at the top of your class file, VB6 requires you to write local implementations of **every single method and property** declared in the interface class. They must be declared as Private, and they follow the naming pattern: InterfaceName_MethodName.

File: CDog.cls

Option Explicit

' Bind this class to the IAnimal contract
Implements IAnimal

' Local class-specific field
Private m_Name As String

Public Property Let Name(ByVal RHS As String)
    m_Name = RHS
End Property

' --- Implementing the IAnimal Interface members ---

Private Property Get IAnimal_Species() As String
    IAnimal_Species = "Canine (Dog)"
End Property

Private Sub IAnimal_MakeSound()
    ' Custom implementation logic
    MsgBox m_Name & " says: Woof! Woof!", vbInformation
End Sub
Private Scope Requirement
The interface procedure implementations must be declared as Private inside the implementation module. This ensures the methods can only be reached when the object is explicitly referenced as the interface type, preventing scope namespace pollution on the concrete object itself.

5. Step 3: Polymorphism in Action

Now that you have your interface and concrete implementations, you can invoke their code polymorphically. You do this by casting your concrete instances to the interface type, and calling methods directly on the interface object.

File: Module1.bas or form code

Public Sub Main()
    ' Declare concrete instances
    Dim DogInstance As CDog
    Dim CatInstance As CCat ' (Assuming a CCat class implements IAnimal too)
    
    Set DogInstance = New CDog
    DogInstance.Name = "Rex"
    
    Set CatInstance = New CCat
    
    ' --- Polymorphic List ---
    Dim Animals(1) As IAnimal
    
    ' Cast to IAnimal interface (VB6 handles query-interface casting implicitly)
    Set Animals(0) = DogInstance
    Set Animals(1) = CatInstance
    
    ' --- Execute Interface logic ---
    Dim i As Integer
    For i = 0 To 1
        ' These calls route to CDog and CCat automatically at runtime
        WScript.Echo "Species: " & Animals(i).Species
        Call Animals(i).MakeSound()
    Next i
End Sub

6. Reusing Code: The Composition Pattern

Because VB6 lacks implementation inheritance, you cannot inherit code logic from a base class. If you want to reuse code instead of duplicating it, you must employ the **Composition** pattern (often described as "has-a" instead of "is-a").

Under composition, you delegate calls from a wrapper class down to an internally held helper class instance.

Example: Simulating a Base Class

Imagine you have common registration functionality that both CDog and CCat need. Instead of rewriting it, you create a shared class called CRegistryHelper and reference it internally:

File: CDog.cls (using Composition)

Option Explicit
Implements IAnimal

' Internal helper instance
Private m_Registry As CRegistryHelper

Private Sub Class_Initialize()
    ' Instantiate the helper internally
    Set m_Registry = New CRegistryHelper
End Sub

Private Sub IAnimal_Register()
    ' Delegate the action to the helper class
    Call m_Registry.RegisterObject(Me)
End Sub

By coupling **Interface Inheritance** (for polymorphism) with **Composition** (for code reuse), you obtain all the architectural benefits of standard object-oriented inheritance models without risking fragile base class bugs.

© 2026 VB6 Advanced OOP Tutorials. Created for GolemScript Integration.

Comments

Popular posts from this blog