Home >>VB.Net Built-in Functions >VB.Net Program of method overloading with type demotion

VB.Net Program of method overloading with type demotion

Write a VB.Net Program of method overloading with type demotion

Here, depending on the different number of arguments, we will overload the Add() method, here we called an overloaded method of type demotion, which means a method that accepts integer argument, but instead of the integer, we will pass long argument.

Program :

The source code is given below to demonstrate the overloading method with type demotion. The program given is compiled and successfully executed.

'VB.net program to demonstrate the method overloading

'with type demotion.


Module Module1
    Class Sample
        Public Sub Add(ByVal num1 As Integer, ByVal num2 As Integer)
            Dim sum As Integer = 0
            sum = num1 + num2
            Console.WriteLine("Sum is: {0}", sum)
        End Sub
        Public Sub Add(ByVal num1 As Long, ByVal num2 As Integer, ByVal num3 As Integer)
            Dim sum As Integer = 0
            sum = num1 + num2 + num3
            Console.WriteLine("Sum is: {0}", sum)
        End Sub
    End Class
    Sub Main()
        Dim obj As New Sample()
        Dim num1 As Long = 15
        Dim num2 As Integer = 30
        Dim num3 As Integer = 45
        obj.Add(num1, num2)
        obj.Add(num1, num2, num3)
    End Sub
End Module
	
Output:
Sum is: 45
Sum is: 90
Explanation:

We created a Module1 module in the program above. Here, to determine the sum of arguments, we created a Sample class that contains two Add() methods.

We have created the Add() method here for two and three arguments, adding the value of the arguments in both methods, and then printing the addition on the console screen.

obj.Add(num1, num2)

In the above code, instead of integer, we passed a long argument, which shows we can perform type demotion in process overloading.


No Sidebar ads