Friday 18 May 2012

Object Oriented Programming in VB.Net (Cont.)


Polymorphism

Polymorphism basically stands for " one name , multiple behaviour" in general concept. It is a important feature of OOPS , In programming it is also called overloading, overloading means the use of same thing for different purposes.
By using of this polymorphism feature we can create many function with the same name, and that all function will show different behavior in different scenarios, but with different argument list.
and the function will perform different operations based on argument list  in function call.

The following code demonstrates the implementation of Polymorphism.



Module Module1


    Sub Main()
        Dim two As New One()
        Console.WriteLine(two.addition(5))
        Console.WriteLine(two.addition(5, 10))
        Console.WriteLine(two.addition(5, 10, 20))
        Console.Read()
    End Sub


End Module


Public Class One
    Public x, y, z As Integer


    Public Function addition(ByVal x As Integer) As Integer
        'function with one argument
        Return x
    End Function


    Public Function addition(ByVal x As Integer, ByVal y As Integer) As Integer
        'function with two arguments
        Return x + y
    End Function


    Public Function addition(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer) As Integer
        'function with three arguments
        Return x + y + z
    End Function


End Class



Output of the above code is shown in the image below.

No comments:

Post a Comment