My solution for abstract class in vba:
View
'-------------------------
' Standard module
Sub Main()
Dim objC1 As C1
Dim objC2 As C2
Dim objCollection As New Collection
Set objC1 = New C1
Set objC2 = New C2
With objC1
.getInterface.M1 "Hello C1!"
temp1 = .getInterface.M2(objCollection)
.getSuperClass.SM1 "Hi C1!!!"
temp3 = .getSuperClass.SM2(objCollection)
End With
Debug.Print vbCrLf
With objC2
.getInterface.M1 "Hello C2!"
temp1 = .getInterface.M2(objCollection)
.getSuperClass.SM1 "Hi C2!!!"
temp3 = .getSuperClass.SM2(objCollection)
End With
End Sub
' -----------------------------------------------
' IAbstracat class module
Sub SM1(strString As String): End Sub
Function SM2(varItem As Variant) As String: End Function
' -----------------------------------------------
' Abstracat class module
Implements IAbstract
'Each class must implement these methods, in a particular way
Sub M1(strString As String): End Sub
Function M2(varItem As Variant) As String: End Function
'The sub classes will extend SM1 and SM2
Private Sub IAbstract_SM1(strString As String)
Debug.Print "Sub IAbstract_SM1: " & strString
End Sub
'The sub classes will extend SM1 and SM2
Private Function IAbstract_SM2(varItem As Variant) As String
Dim strMyString As String
strMyString = "Function IAbstract_SM2 => ObjPtr(varItem): " & ObjPtr(varItem)
Debug.Print strMyString
IAbstract_SM2 = strMyString
End Function
' -----------------------------------------------
' C1 class module
Implements Abstract
Private Type TC1
objConcretSuperClass As Abstract
objInterfaceSuperClass As IAbstract
objInterfaceSubClass As Abstract
End Type
Private this As TC1
'if you do not need to initialize anything, then this is it:
Private Sub Class_Initialize()
With this
'creating an instance of class' Abstract'
Set .objConcretSuperClass = New Abstract
'Referencing the' Abstract 'interface, where are the extended methods
Set .objInterfaceSuperClass = .objConcretSuperClass
'Creating a refence for the C1 interface, which is the class' Abstract'
'Here we have the particular implementations of M1 and M2
Set .objInterfaceSubClass = Me
End With
End Sub
'With this we can do:
' set objC1 = New C1
' objC1.getInterface.Abstract_M1
' objC1.getInterface.Abstract_M2
Property Get getInterface() As Abstract
Set getInterface = this.objInterfaceSubClass
End Property
'With this we can call the methods defined in' Abstract ': SM1 and SM2
' set objC1 = New C1
' objC1.getSuperClass.SM1 "hello!"
' temp = objC1.getSuperClass.SM2 (New Collection)
Property Get getSuperClass() As IAbstract
Set getSuperClass = this.objInterfaceSuperClass
End Property
'Here we have the particular implementations of M1 and M2
Private Sub Abstract_M1(strString As String)
Debug.Print "Class C1 => Sub Abstract_M1: " & strString
End Sub
Private Function Abstract_M2(varItem As Variant) As String
Debug.Print "Class C1 => Function Abstract_M2: " & ObjPtr(varItem)
End Function
' -----------------------------------------------
' C2 class module
Implements Abstract
Private Type TC2
objConcretSuperClass As Abstract
objInterfaceSuperClass As IAbstract
objInterfaceSubClass As Abstract
End Type
Private this As TC2
'if you do not need to initialize anything, then this is it:
Private Sub Class_Initialize()
With this
'creating an instance of class' Abstract'
Set .objConcretSuperClass = New Abstract
'Referencing the' Abstract 'interface, where are the extended methods
Set .objInterfaceSuperClass = .objConcretSuperClass
'Creating a refence for the C1 interface, which is the class' Abstract'
'Here we have the particular implementations of M1 and M2
Set .objInterfaceSubClass = Me
End With
End Sub
'With this we can do:
' set objC2 = New C2
' objC2.getInterface.Abstract_M1
' objC2.getInterface.Abstract_M2
Property Get getInterface() As Abstract
Set getInterface = this.objInterfaceSubClass
End Property
'With this we can call the methods defined in' Abstract ': SM1 and SM2
' set objC1 = New C1
' objC1.getSuperClass.SM1 "hello!"
' temp = objC1.getSuperClass.SM2 (New Collection)
Property Get getSuperClass() As IAbstract
Set getSuperClass = this.objInterfaceSuperClass
End Property
'Here we have the particular implementations of M1 and M2
Private Sub Abstract_M1(strString As String)
Debug.Print "Class C2 => Sub Abstract_M1: " & strString
End Sub
Private Function Abstract_M2(varItem As Variant) As String
Debug.Print "Class C2 => Function Abstract_M2: " & ObjPtr(varItem)
End Function
Immediate Check Window (CTRL + G):
Class C1 => Sub Abstract_M1: Hello C1!
Class C1 => Function Abstract_M2: 550324728
Sub IAbstract_SM1: Hi C1!!!
Function IAbstract_SM2 => ObjPtr(varItem): 550324728
Class C2 => Sub Abstract_M1: Hello C2!
Class C2 => Function Abstract_M2: 550324728
Sub IAbstract_SM1: Hi C2!!!
Function IAbstract_SM2 => ObjPtr(varItem): 550324728