Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic
MustInherit and NotInheritable in VB.NET
Page 2 - A Working Example of Class Inheritance
 Related Resources
• 
Need a "getting started" guide for VB.NET?

• 
Upgrading to VB.NET

• 
Get VB.NET Cheap!

 

To see these keywords in action, a console application that I call "WorkInAmerica" was written. Console applications are a great way to start learning objects in VB.NET because you don't get confused by the complications of forms, graphics, components and tools. You can code pure objects and nothing but objects. Jesse Liberty bases his book, Learning VB.NET, on console applications for that reason.

Here's the code:

Module WorkInAmerica
    Sub Main()
        Const FedMinWage As Decimal = 5.15
        Const WorkDayHours As Decimal = 8
        Dim MyWage As Decimal = FedMinWage
        Dim MyBoss As New BossTheCompany(FedMinWage)
        Dim MyJob As New WorkInTheCompany(FedMinWage)
        Dim GoToWork As New WorkingClass(WorkDayHours)
        Dim MyOwnWork As Decimal = 8
        MyBoss.DoHalfTheWork(4)
        GoToWork.DoSomeWork(4)
        MyBoss.DoTheOtherHalf(4)
        GoToWork.DoSomeWork(4)
        GoToWork.DoSomeWork(MyOwnWork)
        GoToWork.EndWorkDay()
        Console.ReadLine()
    End Sub
End Module

Public Class BossTheCompany
    Inherits BossClass
    Public Sub New(ByVal WorkerWage As Decimal)
        MyBase.New(WorkerWage)
    End Sub
    Public Overrides Sub DoHalfTheWork( _
        ByVal HoursOfWork As Integer)
        ' Do half the boss's work
        Console.WriteLine( _
        "Doing " & Format(HoursOfWork) & _
        " hours of the boss's work!")
    End Sub
    Public Overrides Sub DoTheOtherHalf( _
        ByVal MoreHoursOfWork As Integer)
        ' Now do the other half
        Console.WriteLine("Doing " & _
        Format(MoreHoursOfWork) & _
        " more hours of the boss's work!")
    End Sub
End Class

Public Class WorkInTheCompany
    Public Sub New(ByVal WorkerWage As Decimal)
        Const FedTax As Decimal = 0.35
        Const BenefitsDeduction As Decimal = 0.2
        Dim ActualWage As Decimal = WorkerWage - _
            (WorkerWage * FedTax) - _
            (WorkerWage * BenefitsDeduction)
        Console.WriteLine( _
        "A new worker was hired! The worker gets: " & _
        FormatCurrency(ActualWage) & " per hour!")
    End Sub
End Class

Public MustInherit Class BossClass
    Private BossWage As Decimal = WorkerWage * 400
    Public Sub New(ByVal WorkerWage As Decimal)
        Console.WriteLine( _
            "A new boss was hired! The boss gets: " & _
            FormatCurrency(BossWage) & " per hour!")
    End Sub
    Public MustOverride Sub DoHalfTheWork( _
        ByVal Workload As Integer)
    Public MustOverride Sub DoTheOtherHalf( _
        ByVal Morework As Integer)
End Class

Public NotInheritable Class WorkingClass
    Dim WorkTimeLeft As Integer
    Dim NormalWorkHours As Integer
    Public Sub New(ByVal WorkHours As Decimal)
        Console.WriteLine("Go to work")
        WorkTimeLeft = WorkHours
        NormalWorkHours = WorkHours
        Console.WriteLine( _
            "Expected work day is: " & _
            Format(WorkHours))
    End Sub
    Public Sub DoSomeWork(ByVal WorkHours As Integer)
        WorkTimeLeft -= WorkHours
        Console.WriteLine( _
            "Time left to get work done: " & _
            Format(WorkTimeLeft))
    End Sub
    Public Sub EndWorkDay()
        Dim ActualHours As Integer
        ActualHours = NormalWorkHours - WorkTimeLeft
        Console.WriteLine("Actual work day is: " & _
            Format(ActualHours))
    End Sub
End Class

Note that BossClass is marked MustInherit. For this reason, the two sub's in BossClass - DoHalfTheWork and DoTheOtherHalf are modified by the keyword MustOverride. But BossClass doesn't do any work. There isn't even an End Sub. VB.NET expects that any work will actually be done by a subclass. WorkingClass, on the other hand, is marked NotInheritable and there's a lot of work done there.

When the class MyBoss is instantiated, it's necessary for BossTheCompany to inherit from BossClass. Notice that BossWage is created by the superclass BossClass as a Private variable. But the MyBoss workload is taken care of in the directly instantiated BossTheCompany class. The ability to separate function and protect some methods and properties is one reason programs are designed this way. A normal MyJob class is also created but since this class doesn't inherit anything, all the code to create it is in the class code along with any other work the class does. The GoToWork class must be instantiated (not inherited) because the WorkingClass class is marked NotInheritable. Once all classes have been created, subroutines are called in them to do actual work in the project.

The Console.ReadLine() statement at the end of the Main subroutine is just there to suspend execution so the console window that the program runs in doesn't flash off the screen before you can look at the output.

The Output of the Program

Hopefully, this simple example demonstrates how inheritance can help different classes get along and work well together!

First Page

Explore Visual Basic

By Category

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Visual Basic

  1. Home
  2. Computing & Technology
  3. Visual Basic

©2009 About.com, a part of The New York Times Company.

All rights reserved.