Class Inheritance (For Beginners)

Begginnerdev 3 Tallied Votes 219 Views Share

Upon aiding a friend - I have decided to post the example used here.
The following is a snippet that shows basic class inheritance.

The source file as well as a test case can be found attached.

You can test these classes with the following code:

    Dim cNew As New Car
    Dim vNew As New Van
    Dim tNew As New Truck

    With cNew
        .Make = "Chevrolet"
        .Model = "Camaro"
        .Engine = 6.0
        .Year = 2013
        .Style = Car.StyleOption.Coupe
        MsgBox(.ToString)
    End With

    With vNew
        .Make = "Chevrolet"
        .Model = "Express"
        .Engine = 6.0
        .Passengers = 4
        .Year = 2013
        .Cargo_Rack = True
        .Folding_Seats = False
        MsgBox(.ToString)
    End With

    With tNew
        .Make = "Chevrolet"
        .Model = "Silverado"
        .Engine = 6.0
        .Year = 2013
        .Cab = Truck.CabSize.Crew_Cab
        .Torque = 413
        MsgBox(.ToString)
    End With
TnTinMN commented: What? No Fuel Type on Vehicle. :) +6
Public Class Vehicle
    Private sMake As String = ""
    Private sModel As String = ""
    Private iYear As Integer = 1900
    Private dEngineSize As Double = 3.0

    ''' <summary>
    ''' Gets/Sets the make of the vehicle
    ''' </summary>
    ''' <value>String</value>
    ''' <returns>sMake</returns>
    ''' <remarks></remarks>
    Public Property Make As String
        Get
            Return Me.sMake
        End Get
        Set(value As String)
            Me.sMake = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets the model of the vehicle
    ''' </summary>
    ''' <value>String</value>
    ''' <returns>sModel</returns>
    ''' <remarks></remarks>
    Public Property Model As String
        Get
            Return Me.sModel
        End Get
        Set(value As String)
            Me.sModel = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets the year the vehicle was made.
    ''' </summary>
    ''' <value>Integer</value>
    ''' <returns>iYear</returns>
    ''' <remarks></remarks>
    Public Property Year As Integer
        Get
            Return Me.iYear
        End Get
        Set(value As Integer)
            Me.iYear = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets the value indicating the size of the engine.
    ''' </summary>
    ''' <value>Double</value>
    ''' <returns>dEngineSize</returns>
    ''' <remarks></remarks>
    Public Property Engine As Double
        Get
            Return Me.dEngineSize
        End Get
        Set(value As Double)
            Me.dEngineSize = value
        End Set
    End Property

    ''' <summary>
    ''' Provides a blank constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' Generates a string based on class variables.
    ''' </summary>
    ''' <returns>The class' variables in a string</returns>
    ''' <remarks></remarks>
    Public Overridable Shadows Function ToString() As String
        Return "Year: " & Year & vbCrLf & _
               "Make: " & Make & vbCrLf & _
               "Model: " & Model & vbCrLf & _
               "Engine: " & Engine
    End Function
End Class

Public Class Car
    Inherits Vehicle

    Private sStyle As StyleOption = StyleOption.Coupe

    Public Enum StyleOption
        Coupe
        Sedan
        Luxury
    End Enum

    Public Property Style As StyleOption
        Get
            Return Me.sStyle
        End Get
        Set(value As StyleOption)
            Me.sStyle = value
        End Set
    End Property

    ''' <summary>
    ''' Provides a blank constructor.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' Generates a string based on class variables.
    ''' </summary>
    ''' <returns>The class' variables in a string</returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Return MyBase.ToString & vbCrLf & _
            "Style: " & Style
    End Function
End Class

Public Class Truck
    Inherits Vehicle

    Private iTorque As Integer = 2
    Private dBedSize As Double = 6.0
    Private csNew As CabSize = CabSize.Single_Cab
    Public Enum CabSize
        Single_Cab
        Extended_Cab
        Crew_Cab
    End Enum

    ''' <summary>
    ''' Gets/Sets the integer value indicating the lbs/ft torque the truck boasts.
    ''' </summary>
    ''' <value>Integer</value>
    ''' <returns>iTorque</returns>
    ''' <remarks></remarks>
    Public Property Torque As Integer
        Get
            Return Me.iTorque
        End Get
        Set(value As Integer)
            Me.iTorque = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets the cabsize of the truck.
    ''' </summary>
    ''' <value>Enum</value>
    ''' <returns>csNew</returns>
    ''' <remarks></remarks>
    Public Property Cab As CabSize
        Get
            Return Me.csNew
        End Get
        Set(value As CabSize)
            Me.csNew = value
        End Set
    End Property

    ''' <summary>
    ''' Provides a blank constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' Generates a string based on class variables.
    ''' </summary>
    ''' <returns>The class' variables in a string</returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Return MyBase.ToString & vbCrLf & _
            "Bed Size: " & dBedSize & vbCrLf & _
            "Torque : " & Torque
    End Function
End Class

Public Class Van
    Inherits Vehicle

    Private iPassengers As Integer = 0
    Private bFoldingSeats As Boolean = False
    Private bCargoRack As Boolean = False

    ''' <summary>
    ''' Gets/Sets the interger value indicating total number of passengers.
    ''' </summary>
    ''' <value>Integer</value>
    ''' <returns>iPassengers</returns>
    ''' <remarks></remarks>
    Public Property Passengers As Integer
        Get
            Return Me.iPassengers
        End Get
        Set(value As Integer)
            Me.iPassengers = value
        End Set
    End Property

    ''' <summary>
    ''' Gets/Sets the value indicating if foldable seats are present.
    ''' </summary>
    ''' <value>Boolean</value>
    ''' <returns>bFoldingSeats</returns>
    ''' <remarks></remarks>
    Public Property Folding_Seats As Boolean
        Get
            Return Me.bFoldingSeats
        End Get
        Set(value As Boolean)
            Me.bFoldingSeats = value
        End Set
    End Property


    ''' <summary>
    ''' Gets/Sets the value indicating if a cargo rack is present.
    ''' </summary>
    ''' <value>Boolean</value>
    ''' <returns>bCargoRack</returns>
    ''' <remarks></remarks>
    Public Property Cargo_Rack As Boolean
        Get
            Return Me.bCargoRack
        End Get
        Set(value As Boolean)
            Me.bCargoRack = value
        End Set
    End Property

    ''' <summary>
    ''' Provides a blank constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub

    ''' <summary>
    ''' Generates a string based on class variables.
    ''' </summary>
    ''' <returns>The class' variables in a string</returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Return MyBase.ToString & vbCrLf & _
               "Passengers: " & Passengers & vbCrLf & _
               "Folding Seats: " & IIf(Folding_Seats, "Yes", "No") & vbCrLf & _
               "Cargo Rack: " & IIf(Cargo_Rack, "Yes", "No")
    End Function
End Class