Inheritance in Visual Basic
Inherits, Overridable and MyBase in a two-class hierarchy.
Dim probes As Sensor() = {New Sensor(), New Thermometer()}
For Each p In probes
Console.WriteLine(p.Describe())
Next
Class Sensor
Public Overridable Function Describe() As String
Return "generic sensor"
End Function
End Class
Class Thermometer
Inherits Sensor
Public Overrides Function Describe() As String
Return MyBase.Describe() & ", tuned for heat"
End Function
End Class
How it works
Overridableinvites replacement;Overridesaccepts.Inherits Sensortakes everything the parent defined.MyBase.Describe()starts from the parent's answer and extends it.
Keywords and builtins used here
ClassDescribeDimEachEndForFunctionInheritsMyBaseNewNextOverridableOverridesPublicReturnSensorStringThermometer
The run, in numbers
- Lines
- 18
- Characters to type
- 378
- Tokens
- 78
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 52 seconds.
Step 2 of 3 in Classes & interfaces, step 17 of 29 in Language basics.