Visual Basic .NET - Method Overriding
Implementation in VB.NET
- Visual Basic .NET (VB.NET) is a powerful, high-level, object-oriented programming language developed by Microsoft.
- It is part of the .NET framework.
- A comprehensive platform to develop and run various types of applications, including web, desktop, mobile, and cloud-based applications.
- VB.NET is designed to be easy to learn and use.
- It is a popular choice for both beginner and experienced programmers or Software developers.
To implement method
overriding in VB.NET, the programmer needs to follow these steps:
- Ensure that the derived class inherits from the base class.
- In VB.NET, the Inherits keyword is used to establish inheritance.
- In the above example, D inherits from B, and it overrides the m1() in the derived class.
- The method m1() in class B must be defined with the overridable keyword.
Class B
Public Overridable Sub m1()
' Base class implementation
End Sub
End Class
Class D
Inherits B
Public Overrides Sub m1()
' Custom implementation in the derived class
' New Implementation
' Inherited Method
End Sub
End Class
The class diagram shown above is implemented in VB.NET. The Person class is defined as follows:
Public Class Person
'fields to store First And Last Name of a person
'Declares as protected to make it accessible in Employeee class
Protected firstName As String
Protected lastName As String
'Constructor to initiliaze fields
Public Sub New(ByVal firstName As String, ByVal lastName As String)
Me.firstName = firstName
Me.lastName = lastName
End Sub
'Displays name In Firstname And Lastname format
'Can be overridden In Sub Class
Public Overridable Sub completeName()
Console.WriteLine(firstName + " " + lastName)
End Sub
End Class
The Employee class is defined by inheriting from the Person class. VB.NET provides inherits keyword used to inherit the super class. The constructor is also defined to pass parameters required by Person class constructor. Java provides MyBase keyword to pass parameters from Employee class(Sub class) to the Person class(Super class) from the constructor of the sub class.
The completeName() method gets inherited from the Person class. It displays the name of the Person in the First and Last name format. It is marked with the Overridable keyword to allow Overriding.
Method Overriding is required when name of the Employee is required in the Last and First name format. It is implemented by redefining completeName() method in the Employee class with the same signature and with the Overrides keyword as demonstrated in the code below:
Public Class Employee
Inherits Person
'It Is compulsory to define constructor in Sub class
'As no default constructor in super class
'Its job Is provide parameters required by super Class constructor
Public Sub New(firstName As String, ByVal lastName As String)
MyBase.New(firstName, lastName)
'VB.Net provides MyBase keyword to pass parameters to super class constructor
End Sub
'Method inherited from Person Is overridden here
'Displays name In Lastname And Firstname format
Public Overrides Sub completeName()
'MyBase.completeName()
Console.WriteLine(lastName + " " + firstName)
End Sub
End Class
Once the Person and the Employee classes are defined then can be tested using the following code.
'code to test the defined Person and Employee Class
Module Module1
Sub Main()
'Creating an instance Or an object
'A reference variable of Super class can refer to
'Objects of Super as well as Sub Class
Dim person As Person
'Refers to object of Person Class
person = New Person("Neeraj", "Chopra")
person.completeName()
'Refers to object of Employee Class
person = New Employee("Neeraj", "Chopra")
person.completeName()
Console.ReadKey()
End Sub
End Module
The output of the above code is:
Chopra NeerajChopra Neeraj
Wondering:
- Why all that lengthy code?
- Why not make change in Person class code to get the work done?
Remember in a typical development Scenario, a team of developers working who may be from different organizations. One developer has developed the Person class and shared .dll file (Dynamic Link Library) with other developers. Source code is not shared therefore code is not available to update it.
What if both the versions of the completeName() is required?
Method Overriding is feature of Object-Oriented Languages to achieve runtime polymorphism. Recall in When Person(Super class) Object refers to the object of the Person class calls the super class version of the completeName() method and when it refers to the object of the Employee class (Sub class) calls the sub class version of the completeName() method.
However, the developer of Person may decide not to allows the users of his/her class to override than the developer will not be marked completeName() method with the Overridable keyword.
Visual Basic .NET - Method Overriding
Reviewed by Syed Hafiz Choudhary
on
September 17, 2023
Rating:
No comments: