Visual Basic .NET Language Abstract Classes


Example

If classes share common functionality you can group this in a base or abstract class. Abstract classes can contain partial or no implementation at all and allow the derived type to override the base implementation.

Abstract classes within VisualBasic.NET must be declared as MustInherit and cannot be instantiated.

Public MustInherit Class Vehicle
     Private Property _numberOfWheels As Integer
     Private Property _engineSize As Integer

     Public Sub New(engineSize As Integer, wheels As Integer)
         _numberOfWheels = wheels
         _engineSize = engineSize
     End Sub

     Public Function DisplayWheelCount() As Integer
         Return _numberOfWheels
     End Function
End Class

A sub type can then inherit this abstract class as shown below:

Public Class Car
    Inherits Vehicle
End Class

Car will inherit all of the declared types within vehicle, but can only access them based upon the underlying access modifier.

Dim car As New Car()
car.DisplayWheelCount()

In the above example a new Car instance is created. The DisplayWheelCount() method is then invoked which will call the base class Vehicles implementation.