Polymorphism

Polymorphism

Polymorphism means same operation may behave differently on different classes. Polymorphism implemented by overloading and overriding. Method Overloading is the polymorphism at compile time where as Method overriding is polymorphism at run time.

Method Overloading
Method with same name but with different arguments is called method overloading. Method Overloading forms compile-time polymorphism because at compile time you know which method will be called because of method signature difference.

Example of Method Overloading:

Namespace MethodOverLoading
”’
”’ Parent Class
”’
”’
Public Class Parent

#Region “public Overloads APIs …”
Public Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from Parent”)
End Sub

Public Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(bv_Message & vbCrLf)
End Sub
#End Region

End Class

”’
”’ Child Class Inherited from public class
”’
”’
Public Class child
Inherits Parent

#Region “public Overloads APIs …”

Public Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from child”)
End Sub

Public Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(bv_Message & vbCrLf)
End Sub
#End Region

End Class

End Namespace

Sub Main()
‘Method Over Loading
Dim obj1 As New MethodOverLoading.Parent()
obj1.helloworld()
obj1.helloworld(“Hello world! calling parent object from Main!”)

Dim obj2 As New MethodOverLoading.child()
obj2.helloworld()
obj2.helloworld(“Hello world! calling child object from Main!”)

Dim obj3 As MethodOverLoading.Parent = New MethodOverLoading.child
obj3.helloworld()
obj3.helloworld(“Hello world! calling parent object created from child class from Main!”)

‘It will throw a run time error
‘Unable to cast object of type ‘MethodOverLoading.Parent’ to type ‘MethodOverLoading.child’.
Dim obj4 As MethodOverLoading.child = New MethodOverLoading.Parent
obj4.helloworld()
obj4.helloworld(“Hello world! calling child object created from parent class from Main!”)

System.Console.ReadLine()

End Sub
//Output
Hello World Default Message! Calling from Parent
Hello world! calling parent object from Main!

Hello World Default Message! Calling from child
Hello world! calling child object from Main!

Hello World Default Message! Calling from Parent
Hello world! calling parent object created from child class from Main!

Method Overriding
Method overriding occurs when child class overrides a method with the same signature in one of its parent class. Method overriding is Run-time polymorphism because you do not know which method it will be calling its depends on that object.

Example of Method Overriding:

Namespace MethodOverRiding

”’
”’ Parent Class
”’
”’
Public Class Parent

#Region “public Overridable APIs …”
Public Overridable Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from Parent”)
End Sub

Public Overridable Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(bv_Message & vbCrLf)
End Sub
#End Region

End Class

”’
”’ Child Class Inherited from public class
”’
”’
Public Class child
Inherits Parent

#Region “public Overrides APIs …”

Public Overrides Sub helloworld()
System.Console.WriteLine(“Hello World Default Message! Calling from child”)
End Sub

Public Overrides Sub helloworld(ByVal bv_Message As String)
System.Console.WriteLine(“From Child Class: ” & bv_Message & vbCrLf)
End Sub
#End Region

End Class

End Namespace

//Output
Hello World Default Message! Calling from Parent
Hello world! calling parent object from Main!

Hello World Default Message! Calling from child
From Child Class: Hello world! calling child object from Main!

Hello World Default Message! Calling from Parent
From Child Class: Hello world! calling parent object created from child class from Main!

Advertisement

One Comment (+add yours?)

  1. Rajat
    Mar 05, 2010 @ 19:11:01

    Thanks Ashish,
    really helpful.

    Keeep posting dear.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.