How to use inheritance in visual basic.net
One of the important feature of object oriented programming is Inheritance. Using this feature, we can build a set of related classes organized in a hierarchy allowing us to use and reuse code. In this article, we will see how to use inheritance to create and use a base class and a derived class in Visual Basic.net.
Programmers who develop software for today's world of web applications, SAS applications and mobile applications need to know what is inheritance and how to implement inheritance. Using inheritance, we can create a class and derive another class from it. We use the Inherits statement to derive another class in Visual basic.Net. (Click here to know How to use inheritance in C#). By deriving a class from a base class, we can extend the functionalities of the base class. The derived class has all the attributes and functionalities of the base class and also its own attributes and functionalities. By implementing inheritance, we use the code present in the base class, in the derived class, without rewriting the code in the derived class.
In this example we define a base class 'Account' which represents all the characteristics of a generic account that exist independent of whether the account is a Customer or Vendor or Bank account. Then we will define a derived class 'BankAccount' class that represents the bank account. The BankAccount class is derived from the Account class, and extends the Account class functionality. The functionality of the BankAccount class is to handle deposits and withdrawals. The BankAccount class uses all the functionalities of the Account class and adds two methods to deposit and withdraw the amount from the specified bank account.
The following code creates the base class - Account Class
Constructor1: Constructor to initialize all the member variables.
A bank account has a cash balance that changes when deposits and withdrawals are made by the account holder. The method deposit(), deposits money into the bank account and takes parameter 'amount'. The method withdraw() withdraws money from the bank account and takes parameter 'amount'. The code for the derived class -BankAccount is given below.
To see how inheritance works, create a windows form and name it as BankForm. Design the form as shown in the Fig below. The code for the BankForm is given below.
The above example demonstrates implementation of single inheritance which is a class derived from only one base class.
Programmers who develop software for today's world of web applications, SAS applications and mobile applications need to know what is inheritance and how to implement inheritance. Using inheritance, we can create a class and derive another class from it. We use the Inherits statement to derive another class in Visual basic.Net. (Click here to know How to use inheritance in C#). By deriving a class from a base class, we can extend the functionalities of the base class. The derived class has all the attributes and functionalities of the base class and also its own attributes and functionalities. By implementing inheritance, we use the code present in the base class, in the derived class, without rewriting the code in the derived class.
In this example we define a base class 'Account' which represents all the characteristics of a generic account that exist independent of whether the account is a Customer or Vendor or Bank account. Then we will define a derived class 'BankAccount' class that represents the bank account. The BankAccount class is derived from the Account class, and extends the Account class functionality. The functionality of the BankAccount class is to handle deposits and withdrawals. The BankAccount class uses all the functionalities of the Account class and adds two methods to deposit and withdraw the amount from the specified bank account.
The following code creates the base class - Account Class
Public Class Account Private mCode As String 'Account code Private mName As String 'Account Name Private mdescription As String 'Account description Protected mBalance As Double 'Account BalanceConstructor of the base class to initialize the fields
Constructor1: Constructor to initialize all the member variables.
Public Sub New(ByVal code, ByVal name, ByVal description, ByVal balance) mCode = code mName = name mdescription = description mBalance = balance End SubCreation of the derived class - BankAccount
A bank account has a cash balance that changes when deposits and withdrawals are made by the account holder. The method deposit(), deposits money into the bank account and takes parameter 'amount'. The method withdraw() withdraws money from the bank account and takes parameter 'amount'. The code for the derived class -BankAccount is given below.
Public Class BankAccount Inherits Account Public Sub deposit(ByVal amount As Double) Dim newBalance As Double newBalance = Balance + amount Balance = newBalance End Sub Public Sub withdraw(ByVal amount As Double) Dim newBalance As Double newBalance = Balance - amount Balance = newBalance End Sub End Class
To see how inheritance works, create a windows form and name it as BankForm. Design the form as shown in the Fig below. The code for the BankForm is given below.

Public Class BankForm Dim myBankAcc As BankAccount = New BankAccount() Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click myBankAcc.Code = txtCode.Text myBankAcc.Name = txtName.Text myBankAcc.description = txtdescription.Text myBankAcc.Balance = txtBalance.Text End Sub Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click myBankAcc.deposit(100) MessageBox.Show(myBankAcc.Getbalance()) End Sub Private Sub btnWithdraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdraw.Click myBankAcc.withdraw(50) MessageBox.Show(myBankAcc.Getbalance()) End Sub End Class
The above example demonstrates implementation of single inheritance which is a class derived from only one base class.
Object Oriented Programming Articles
- Abstract class constructors
- abstract class inheritance in c#.net
- abstract class inheritance in visual basic.net
- New C# Language Feature: Automatic Properties
- Calling base class constructor in C#
- base keyword in c#
- Compile time polymorphism - Method OverLoading
- How to initialize Base class Object from inside Derived Class constructor
- Constructor overloading in vb.net
- How to use inheritance in c#
- How to use inheritance in visual basic.net
- Parameter array in vb.net
- Procedure OverLoading in vb.net
- Visual Inheritance with Windows Forms
- Azure Platform
- GridView CommandField example
- GridView and DetailsView Master/Detail page using SqlDataSource control
- POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
- Query entity data model using linq to entities
- Difference between arraylist and list collection
- How to create a Web service using Visual Studio.net
- FormView DataBound Event
- Calling base class constructor in C#
- Convert a sequence to a generic list using ToList()method
- Project ideas for students
- Accounting Software
- Creating an ASP.Net MVC 3 application
- Using assemblies in .net applications
- How to implement form validation using ASP.Net 2.0 Validation Controls
- Constructors in Visual Basic.Net