stu nicholls dot com | menu - Professional dropdown #3

click here


Polymorphism


Inheritence based Polymorphism

With Inheritance based polymorphism we can store the objects of a derived class in variables of that class’s base class but you can access only the base class’s members using that variables, unless, as mentioned, a derived class has specifically overridden a base class’s method or property
The example given has an Human class with a Dancing method that displays “Dancing” in the MSg Box, and derives a adult class from Human class that overrides Dancing to display “Bharatha Natyam”
A sub procedure Display() takes the argument of the Human class and invokes the Dancing() method
Through polymorphism, the method Display() can be called with the objects of either Human class or adult class.

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim jany As New Human()
Dim rony As New adult()
display(jany)
display(rony)
End Sub
Public Sub display(ByVal HumanObject As Human)
HumanObject.Dancing()
End Sub
End Class
Public Class Human
Overridable Sub Dancing()
MsgBox("Dancing")
End Sub
End Class
Public Class adult
Inherits Human
Overrides Sub Dancing()
MsgBox("Bharatha Natyam")
End Sub
End Class

Interface Based Polymorphism

Interface based polymorphism provide another way to accomplish polymorphism. To support polymorphism with Interfaces, you create an interface and implement it in different ways in various classes. You can then invoke the implemented method of either kind of object in the same way.
So in the following example an Interface named HumanInterface with one method, Dancing is created. This method is implemented in classes named Human and adult

Public Interface HumanInterface
Sub Dancing()
End Interface
Public Class IntPoly
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim jany As New Human()
Dim rony As New adult()
display(jany)
display(rony)
End Sub
Public Sub display(ByVal HumanObject As HumanInterface)
HumanObject.Dancing()
End Sub
End Class
Public Class Human
Implements HumanInterface
Sub Dancing() Implements HumanInterface.Dancing
MsgBox("Dancing")
End Sub
End Class
Public Class adult
Implements HumanInterface
Sub Dancing() Implements HumanInterface.Dancing
MsgBox("Bharatha Natyam")
End Sub
End Class
Getting Rid of objects

When you have done with an object and want to get rid of it so that it no longer takes up memory and other resources. The official way to get rid of objects is to assign them the keyword nothing

Example
Dim notifierObject as New Notifier()
notifierObject.Display(“Hello”)
notifierObject=Nothing

This tags the object for garbage collection. When the .NET Frame works invokes with its Garbage Collection mechanism this object will be destroyed.
Note: You need not do it explicitly to get rid of an object as it is done automatically when it goes out of the scope, it is automatically targeted for Garbage collection

Triggering Garbage Collection

Although Garbage collection mechanism is called automatically it is a little known fact that you do have control over this if you want it. You have to go to Garbage Collector (GC) name space and use the Collect method

Interfaces<< Previous   Next>>Programs


comments powered by Disqus
click here
Footer1