Overloading and Overriding in Visual Basic
Overloading and Overriding in Visual Basic
Basic.NET 2005
(Page 1 of 5 )
This article gives you an in-depth understanding of the differences between overloading and
overriding in Visual Basic.NET 2005. It is assumed that the reader knows something about
object-oriented programming in Visual Basic.NET.
Two downloadable zip files are available for this article, one on overloading and one on
overriding.
If you are new to OOP in Visual Basic.NET, I strongly suggest that you go through the following
links:
Using Constructors with Object-Oriented Database Development
Properties and Object-Oriented Database Development
Using Methods with Object-Oriented Database Development
An Introduction to Object-Oriented Database Development
The entire source code for this article is available in the form of downloadable zip files. The
solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft
Windows Server 2003 Enterprise Edition. I didn't really test it in any other environment. I
request that you post in the discussion area if you have any problems in execution.
A sample class without overloading
Before taking a direct look at overloading, let us try to understand the following class:
Public Class Sample
Private _x As Double
Private _y As Double
End Class
The above class has two private fields ("_x" and "_y") which are only accessible within the class
(and not outside the class). Further, it has two public properties and three public methods (public
members are accessible even outside the class).
The following is the sample code needed to test the above sample class:
Dim obj As New Sample
obj.X = 5
obj.Y = 30
MessageBox.Show("Product = " & obj.GetProduct())
When we have ten parameters to pass, with different combinations, to methods which have
similar logic, naming those methods differently from each other would be confusing when
working with objects. In such a situation, we can implement more methods with the same name
and with a difference in parameters.
A rewrite of the class in the previous section, which implements overloading (more methods
with the same name and different parameters) is as follows:
Public Class Sample2
Private _x As Double
Private _y As Double
End Class
As you can see, for the above class there exist two methods with the same name, "SetValues,"
but with different parameters. Those are called overloaded methods. The following is the code
that tests the above class:
Dim obj As New Sample2
obj.X = 5
obj.Y = 30
MessageBox.Show("Product = " & obj.GetProduct())
Overloading is not limited only to methods. We can even overload constructors, properties, and
so forth. The following is a rewrite of the above class which includes both method and
constructor overloading:
Public Class Sample3
Private _x As Double
Private _y As Double
End Sub
End Class
As you can see from the comments in the above class, I am trying to call overloaded methods
from overloaded constructors, which is possible.
The following is the code to test the constructors in the above class:
Dim obj As New Sample3(10, 20)
MessageBox.Show("Product = " & obj.GetProduct())
In all of the previous sections, we worked with only "overloading." This section focuses on
"overriding."
Before proceeding further, please note that there is no relationship or similarity between
"overloading" and "overriding." They are completely different. To demonstrate "overriding," one
needs to work with inheritance. If you are new to inheritance in Visual Basic.NET, I strongly
suggest that you go through the following article:
Inheritance with VB.NET 2005
Let us start with defining two simple classes as follows:
Public Class Parent
Public Sub DispMsg()
MessageBox.Show("From Parent")
End Sub
End Class
In the previous section, I covered the meaning of overriding. In this section, I shall extend the
same concept further to multi-level inheritance and overriding in Visual Basic 2005.
Let us redefine our classes as follows:
Public Class Parent
Public Overridable Sub DispMsg()
MessageBox.Show("From Parent")
End Sub
End Class