typestar

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

  1. Overridable invites replacement; Overrides accepts.
  2. Inherits Sensor takes everything the parent defined.
  3. MyBase.Describe() starts from the parent's answer and extends it.

Keywords and builtins used here

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.

Type this snippet

Step 2 of 3 in Classes & interfaces, step 17 of 29 in Language basics.

← Previous Next →

Inheritance in other languages