Dear visitor, welcome to Jabaco - Community.
If this is your first visit here, please read the Help. It explains in detail how this page works.
To use all features of this page, you should consider registering.
Please use the registration form, to register here or read more information about the registration process.
If you are already registered, please login here.
call the constructor of a base class in a derived sub-class
Is it possible to call the constructor of a base class in a derived sub-class in Jabaco?
In Java this would be done using "super()".
In general I found it difficult to write parametrized constructors in Jabaco.
It might be easier to live with parameter-less constructors and move the initialization stuff
to some init() method.
Greetings
A1880
Hi,
you could create the default constructor on your own.
class MyBase
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
12
|
Option Explicit
Dim myInt As Integer
'default constructor
Public Sub MyBase()
myInt = 123
End Sub
Public Sub MyBase(i As Integer)
myInt = i
End Sub
Public Function ToString() As String
ToString = CStr(myInt)
End Function
|
class Derived superclass: MyBase
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Option Explicit
Dim myName As String
'default constructor
Public Sub Derived
Base.myInt = 456
myName = "derived"
End Sub
Public Sub Derived(aName As String)
MyName = aName
End Sub
Public Function ToString() As String
ToString = Base.ToString & " - " & CStr(MyName)
End Function
|
regards
OlimilO
example
example
|
Jabaco Source
|
1
2
3
4
5
6
7
8
9
10
11
|
Dim B As New MyBase
MsgBox B.ToString
B = New MyBase(789)
MsgBox B.ToString
Dim D As New Derived
MsgBox D.ToString
D = New Derived("things")
MsgBox D.ToString
|
regards
OlimilO
Yes, that solves the problem! Thanks for your example.
It is not sufficient to define a "Class_Initialize" in the base class or to leav out the default constructor at all.
You have to define your own default constructor. Otherwise Jabaco does not allow you to define constructors of derived classes.
But it is still not possible to call the constructor of a base class.
In your example, you circumvented this limitation by a simple "Base.MyInt = 456".
But for more complex base constructors it would be nice to write "super(456)".
Greetings
A1880