Polymorphism in VB.NET
In this article I will describe Run time Polymorphism and Compile time polymorphism in VB.NET.
Polymorphism:- Polymorphism is one of the crucial features of VB.NET, It means "The ability to take on different form", It is also called as Overloading which means the use of same thing for different purposes. Using Polymorphism we can create as many functions we want with one function name but with different argument list. The function performs different operations based on the argument list in the function call. The exact function to be invoked will be determined by checking the type and number of arguments in the function.
Definition:- "Manipulated the object of various classes and invoke method on one object without knowing the object type".
Example:-polymorphism real word examples are Student, Manager, Animal and etc.
Polymorphism can be divide in to two parts, that are given bellow
- Compile time polymorphism
- Run time polymorphism
Compile time polymorphism:- compile time polymorphism achieved by "Method Overloading", means that same name function with deferent parameters in same class called compile time polymorphism.
Dim two As New One()
'calls the function with one argument
'calls the function with two arguments
WriteLine(two.add(10, 20, 30))
'calls the function with three arguments
Public Class One
Public i, j, k As Integer
Public Function add(ByVal i As Integer) As Integer
'function with one argument
Public Function add(ByVal i As Integer, ByVal j As Integer) As Integer
'function with two arguments
Return i + j
Public Function add(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer) As Integer
'function with three arguments
Return i + j + k
Run time Polymorphism:-Run time polymorphism achieved by "Method Overriding or Operator Overloading", means that same name function with
same parameters in deferent deferent classes called Run time Polymorphism .
Public Class poly
Public Sub show()
Public Class poly2
Public Sub show()
Public Shared Sub Main()
Dim a As poly2 = New poly2()