How to use inheritance in visual

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

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 Balance
Constructor 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 Sub
Creation 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.

windows form named as Bank Form to demonstrate inheritance in

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
    End Sub
    Private Sub btnWithdraw_Click(ByVal sender As System.Object, 	
						ByVal e As System.EventArgs) Handles btnWithdraw.Click
    End Sub
End Class

The above example demonstrates implementation of single inheritance which is a class derived from only one base class.