Pages

Sunday, March 18, 2012

Difference between Static Polymorphis and Dynamic Polymorphism


What is Static Polymorphism?
What is Compile time Polymorphism?
What is Early binding?
What is Dynamic Polymorphism?
What is Runtime time Polymorphism?
What is Late binding?
These are most confusing questions, these things you use when you write code daily. But unless you prepare for interview, if someone ask you then you will be confused with this concepts.
In this small article, I will explain you what is static polymorphism and dynamic polymorphism?
Polymorphism means one name many forms.
Below Image will give you basic idea of static polymorphism and dynamic polymorphism.

Static Polymorphism: - Static Polymorphism means Compile time polymorphism or Early binding.
Method Overloading is an example of Static Polymorphism, where method name is same with different parameters and implementation.
Example:-

Public NotInheritable Class ErrorHandling
    Public Shared Sub WriteErrorToDB(ByVal CustomMessage As String)
        MessageBox.Show(CustomMessage)
    End Sub

    Public Shared Sub WriteErrorToDB(ByVal CustomMessage As String, ByVal ExceptionMessage As String)
        MessageBox.Show(CustomMessage + " And also " + ExceptionMessage)
    End Sub
End Class

Public Class Form3
    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            '   Some logic
            '   Failed Case
            '   Case where ex.message is not empty
            Throw New ApplicationException("Your Code had got failed.")
            '   Case where ex.message is empty
            '   Throw New ApplicationException("")
        Catch ex As Exception
            If Convert.ToString(ex.Message) = "" Then
                '   Case where ex.message is empty
                ErrorHandling.WriteErrorToDB("Their is some logic error in your form3. Please check it.")
            Else
                '   Case where ex.message is not empty
                ErrorHandling.WriteErrorToDB("Their is some logic error in your form3. Please check it.", ex.Message.ToString())
            End If
        End Try
    End Sub
End Class

Dynamic polymorphism: – Dynamic polymorphism means Runtime time polymorphism or Late binding.
Method Overriding is an example of Dynamic Polymorphism, where the base class method is overridden by override keyword and its implementation is also changed.

Example:-

'   Base class of cars which defines certain property of car which all car should have.
'   So all its property will be Overridable
Public MustInherit Class Car
    Public Overridable Sub CarProp()
        MessageBox.Show("Car has four tyres")
    End Sub
End Class

'   A car from Maruti manufacturer, it should be inherited from Car.
Public Class Maruti
    Inherits Car
    Public Overrides Sub CarProp()
        MessageBox.Show("Maruti has four tyres")
    End Sub
End Class

'   A car from Hyundai manufacturer, it should be inherited from Car.
Public Class Hyundai
    Inherits Car
    Public Overrides Sub CarProp()
        MessageBox.Show("Hyundai has four tyres")
    End Sub
End Class

Public Class Form4
    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '   Instance of Car has been created, but at runtime values are assigned to it.
        Dim objCar As Car
        objCar = New Hyundai
        objCar.CarProp()

        objCar = New Maruti
        objCar.CarProp()
    End Sub
End Class





No comments: