Inheritance is an important object-oriented concept. It allows you to build a hierarchy of related classes and to reuse functionality defined in existing classes.
In this article, you will learn how to use inheritance in Microsoft Visual Basic 2005 or in Microsoft Visual Basic .NET. You will define a base class that contains fields and methods for a generic bank account. You will then define a derived class that represents a particular kind of bank account. The derived class will inherit members from the base class, override selective members, and add new members for that type of account.
RequirementsPrior knowledge required:
- Visual Basic 2005 or Visual Basic .NET syntax
- Object-oriented concepts
Create a new console application
- Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET, and create a new Visual Basic console application project named UseInheritance on your local computer.
- On the Project menu, click Add Class.
- In the Add New Item dialog box, type Account.vb in the Name field. Click Open.
Note In Visual Studio 2005, click Add instead of Open.
- Visual Studio .NET displays the file Account.vb.
- Save the project.
Create an abstract base class
- Add the MustInherit modifier to the Account class, to make Account an abstract class:
Public MustInherit Class Account
Write code for the base class
- Add two fields to the Account class:
Private Name As String ' Only accessible in base class
Protected Balance As Double ' Accessible in base class and derived class
- Add a constructor to initialize these fields:
Public Sub New(ByVal Nm As String, ByVal Bal As Double)
Name = Nm
Balance = Bal
- Add the following methods to the class. The Overridable keyword means these methods can be overridden in derived classes:
Public Overridable Sub Credit(ByVal Amount As Double)
Balance += Amount
Public Overridable Sub Debit(ByVal Amount As Double)
Balance -= Amount
Public Overridable Sub Display()
Console.WriteLine("Name=" & Name & ", " & "Balance=" & Balance)
- Add the following method to the class. Because this method is not marked as Overridable, it cannot be overridden in derived classes. This method provides the capability to change the name of the account holder.
Public Sub ChangeName(ByVal newName As String)
Name = newName
- Add the following method to the class. The MustOverride keyword means this method must be overridden in derived classes:
Public MustOverride Function CalculateBankCharge() As Double
Create a derived class
- From the Project menu, click Add Class.
- In the Add New Item dialog box, type SavingsAccount.vb in the Name field. Click Open.
- Visual Studio .NET displays the file SavingsAccount.vb.
- Change the SavingsAccount class definition as follows, so that SavingsAccount inherits from Account (note that the Inherits keyword must appear on a new line):
Public Class SavingsAccount
Write code for the derived class
- Add a field to the SavingsAccount class:
Private MinBalance As Double ' If the balance drops below MinBalance,
' the bank will charge a fee on the account
- Add a constructor as follows, to initialize the fields in the base class and in this class:
Public Sub New(ByVal Nm As String, _
ByVal Bal As Double, _
ByVal Min As Double)
MyBase.New(Nm, Bal) ' Call base-class constructor first
MinBalance = Min ' Then initialize fields in this class
- Add the following methods to the SavingsAccount class. These methods override the Overridable methods inherited from the base class:
Public Overrides Sub Debit(Amount As Double)
If Amount <= Balance Then ' Use balance, inherited from base class
MyBase.Debit(Amount) ' Call Debit, inherited from base class
Public Overrides Sub Display()
MyBase.Display() ' Call Display, inherited from base class
Console.WriteLine("$5 charge if balance goes below $" & MinBalance)
- You must override all MustOverride methods from the base class. Add the following method to the SavingsAccount class:
Public Overrides Function CalculateBankCharge() As Double
If Balance < MinBalance Then
Verify that it works
- Display the code for Module1.vb in the Code View window.
- In the Main method, create a SavingsAccount object as follows:
Dim sa As SavingsAccount = New SavingsAccount("Freda Smith", 100.00, 25)
- Add the following code to call Public methods in SavingsAccount or Account:
Console.WriteLine("Bank charge: $" & sa.CalculateBankCharge())
- Build the application.
- From the Debug menu, click Start Without Debugging to run the application. The application displays the following information on the console:
Name=Freda Smith, balance=100
$5 charge if balance goes below $25
Name=Freda Jones, balance=20
$5 charge if balance goes below $25
Bank charge: $5
- Run the application again, but this time use the debugger. Set a breakpoint at the start of the Main method and select Start from the Debug menu. Step into each statement and observe which methods are called during the application.
Article ID: 307222 - Last Review: December 6, 2006 - Revision: 4.4
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
|kbvs2005applies kbvs2005swept kbhowtomaster KB307222|