Polymorphism in Visual Basic .NET

In this article, I will explain you about Polymorphism and it's implementation in Visual Basic .NET.
  • 3140

In this article, I will explain you about Polymorphism and it's implementation in Visual Basic .NET.

Polymorphism

Polymorphism is one of the key concept of Object-Oriented Programming. It is the ability of programming language to process objects depending on their datatype or class differently. It is the process of using same thing for different purposes which also means Overloading. With the help of the polymorphism we can create more then one functions with same function name but with different argument list. The function acts differently based on the argument list in the function call. The type and number of arguments in the functions will determines that exactly which function is invoked.

The following code show you the implementation of Polymorphism:

Imports System.Console
Module Module1

    Sub Main()
        Dim Obj As New Polymorphism()
        WriteLine(Obj.Sum(20))
        'calls the function with one argument
        WriteLine(Obj.Sum(20, 40))
        'calls the function with two arguments
        WriteLine(Obj.Sum(20, 40, 60))
        'calls the function with three arguments
        Read()
    End Sub
 
End Module
 
Public Class Polymorphism
    Public A, B, C As Integer
 
  
    Public
Function Sum(ByVal A As Integer) As Integer

        'function with one argument
        Return A
    End Function
 
    Public Function Sum(ByVal A As Integer, ByVal B As Integer) As Integer
        'function with two arguments
        Return A + B
    End Function
 
    Public Function Sum(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer) As Integer
        'function with three arguments
        Return A + B + C
    End Function
 
End Class

The output of the above code is:

Output3.gif

Summary

Hope this article help you to learn Polymorphism and it's implementation in Visual Basic.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.