
What is Polymorphism?
Tarık Ateş
Smart Grid Solutions Manager
Aktif Mühendislik
Polymorphism refers to what is meant by the term and how it is used in object-oriented software development languages.
WHAT IS POLYMORPHISM?
The term is formed by the combination of the words “very” (πολύ) and “form” (μορφή) in Greek. Polymorphism can be called as the presence of more than one form at the same time in the same habitat according to biology science. [1]
In object-oriented programming languages, polymorphism is that methods can behave differently in new classes derived from the same base class. [2]
If we explain with an example; we can make two types of reading from the energy meters via communication port basically as instant (DRO – Data Read Out) and load profile (LP – Load Profile). However, it is not possible for advanced meter manufacturers such as Iskraemeco have a lot of load profiles can be found like Magnetic field detection, Loss/Leak detection and more…
That is to say, polymorphism is utilized in such points as basically the same species but in different types of points where the same methods must behave differently.
Now, if we prepare a code on this subject;
1. Public Class MeterBase 2. Public Overridable Function ReadDRO() As String 3. Return "instant data was read successfully." 4. End Function 5. 6. Public Overridable Function ReadLP(ByVal FirstDate As Date, ByVal LastDate As Date) As String 7. Return " Load profile was successfully read between two dates." 8. End Function
ENERGYMETER CLASS WHICH IS CREATED BY METERBASE
Unlike the basic energy meters, the X energy meter brand which is described overloads the ReadLP function because it gives the load profile as the number of records rather than the data between two dates.
1. Public Class MeterX : Inherits MeterBase 2. 3. Public Overloads Function ReadLP(ByVal LastRegisterCount As Integer) As String 4. Return String.Format("Reading the last {0} load profile data from the X brand was made. ",LastRegisterCount) 5. 6. End Function
ISKRAEMECO ENERGYMETER CLASS WHICH IS CREATED BY METERBASE
Since Iskraemeco meter brand produces advanced meter types, unlike the basic meter, it supports many additional load profiles in addition to the existing load profile. For this reason, the ReadLP function is overloaded and new private functions are added in addition to the ReadLP function in the MeterBase class.
1. Public Class MeterIskraemeco : Inherits MeterBase 2. 3. Public Overloads Function ReadLP(ByVal FirstDate As Date, ByVal LastDate As Date) As String 4. Dim RetVal As String = String.Empty 5. RetVal = MyBase.ReadLP(FirstDate, LastDate) 6. RetVal += vbNewLine & ReadFraudDetection(FirstDate, LastDate) 7. RetVal += vbNewLine & ReadPowerQuality(FirstDate, LastDate) 8. RetVal += vbNewLine & ReadMagneticTamper(FirstDate, LastDate) 9. Return RetVal 10. End Function 11. 12. Private Function ReadFraudDetection(ByVal FirstDate As Date, ByVal LastDate As Date) As String 13. Return "The leak detection profile between two dates was successfully read." 14. End Function 15. 16. Private Function ReadPowerQuality(ByVal FirstDate As Date, ByVal LastDate As Date) As String 17. Return " The power quality profile between two dates was successfully read." 18. End Function 19. 20. Private Function ReadMagneticTamper(ByVal FirstDate As Date, ByVal LastDate As Date) As String 21. Return " Magnetic field detection profile between two dates was successfully read." 22. End Function
MAIN PROJECT TO WORK THREE CLASS
Although the ReadDRO and ReadLP functions are available in three classes, but the ReadLP function works differently in each class. Also, thanks to the polymorphism, the ReadDRO function is written only in the MeterBase class, although it does not have to be written again in all derived classes.
1. Public Class frmMain 2. Dim mBaseMeter As New PolimorfizmHelper.MeterBase 3. Dim mXMeter As New 4. PolimorfizmHelper.MeterX 5. Dim mIskraemecoMeter As New PolimorfizmHelper.MeterIskraemeco 6. 7. Private Sub btnDRO_Click(sender As Object, e As EventArgs) Handles btnDRO.Click 8. txtLog.Text = String.Empty 9. txtLog.Text += "Base Energy Meter " & mBaseMeter.ReadDRO 10. txtLog.Text += vbNewLine & "X energy meter " & mXMeter.ReadDRO 11. txtLog.Text += vbNewLine & "Iskraemeco energy meter " & mIskraemecoMeter.ReadDRO 12. End Sub 13. 14. Private Sub btnLP_Click(sender As Object, e As EventArgs) Handles btnLP.Click 15. 16. txtLog.Text = String.Empty 17. 18. Dim mFirstDate = New Date(Now.Year, Now.Month, 1) 19. Dim mLastDate = New Date(Now.Year, Now.Month, 1) 20. 21. txtLog.Text += "Base energy meter " & mBaseMeter.ReadLP(mFirstDate, mLastDate) 22. txtLog.Text += vbNewLine & "X brand energy meter " & mXMeter.ReadLP(mFirstDate, mLastDate) 23. txtLog.Text += vbNewLine & vbNewLine & vbNewLine & "Iskraemeco energy meter; " & vbNewLine & mIskraemecoMeter.ReadLP(mFirstDate, mLastDate) 24. End Sub 25. End Function
REFERENCES
[2] Çok biçimlilik – Vikipedi
[3] Microsoft – How Visual Basic Provides Polymorphism