![]() ![]() | ||
Polymorphism is the ability to assume different forms. Polymorphism lets you assign objects of a derived class to variables of the class's base class; we've already seen that like this, where I've assigned an object of the Dog class (which is derived from Animal) to a variable of the Animal class (the Dog class's base class):
Dim obj As Animal Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click obj = New Dog(Me) obj.Breathing ⋮ End Sub
Now I can use the Animal class's members with obj, even though I've stored a Dog class object in obj. I cannot, however, use any members that are not part of the Animal class with obj, such as Barking, which is a method added to Animal in the Dog class (unless the derived class has specifically overridden a base class's method or property, in which case Visual Basic will use the new, overriding version and not the base class version). This is useful, because, for example, you can use one routine to handle objects of a base class and all classes derived from that base class. There are two ways to handle polymorphism in Visual Basic—inheritance-based polymorphism, and interface-based polymorphism. I'll take a look at them here. (You can see both of these techniques in the Polymorphism example on the CD-ROM.)
Inheritance-based polymorphism works as we've already seen—you can store 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 variable, unless, as mentioned, a derived class has specifically overridden a base class's method or property). Here's how that works in the Polymorphism example on the CD-ROM; there, I declare the Animal class with a Breathe method that displays "Breathing…" in a message box, and derive a Fish class from Animal that overrides Breathe to display "Bubbling…":
Public Class Animal Overridable Sub Breathe() MsgBox("Breathing...") End Sub End Class Public Class Fish Inherits Animal Overrides Sub Breathe() MsgBox("Bubbling...") End Sub End Class
Here's where the quality of polymorphism comes in handy; I can set up a Sub procedure named Display that takes an argument of the Animal class and invokes its Breathe method:
Public Sub Display(ByVal AnimalObject As Animal) AnimalObject.Breathe() End Sub
Through polymorphism, I can call Display with objects of either the Animal or Fish class:
Public Class Form1 Inherits System.Windows.Forms.Form 'Windows Form Designer generated code Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim pet1 As New Animal() Dim pet2 As New Fish() Display(pet1) Display(pet2) End Sub Public Sub Display(ByVal AnimalObject As Animal) AnimalObject.Breathe() End Sub End Class
You can see the results in Figure 12.5 when you click the "Inheritance-based Polymorphism" button.
Interfaces provide another way you can accomplish polymorphism in Visual Basic .NET. 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. Here's how that works in the Polymorphism example on the CD-ROM; in this case, I create an interface named AnimalInterface with one method, Breathe, and implement that method in classes named Animal2 and Fish2:
Public Interface AnimalInterface Sub Breathe() End Interface Public Class Animal2 Implements AnimalInterface Sub Breathe() Implements AnimalInterface.Breathe MsgBox("Breathing...") End Sub End Class Public Class Fish2 Implements AnimalInterface Sub Breathe() Implements AnimalInterface.Breathe MsgBox("Bubbling...") End Sub End Class
Now I can use one method, Display2, to handle both Animal and Fish objects if I pass it an argument of the AnimalInterface type:
Public Sub Display2(ByVal AnimalObject As AnimalInterface) AnimalObject.Breathe() End Sub
Here's how that looks in the Polymorphism example on the CD-ROM. In this case, when the user clicks the "Interface-based Polymorphism" button, the code creates an Animal2 object and a Fish2 object, and calls Display2 with both of them:
Public Class Form1 Inherits System.Windows.Forms.Form 'Windows Form Designer generated code Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Dim pet1 As New Animal2() Dim pet2 As New Fish2() Display2(pet1) Display2(pet2) End Sub Public Sub Display2(ByVal AnimalObject As AnimalInterface) AnimalObject.Breathe() End Sub End Class
The results of this code look just as you see in Figure 12.5, showing that you can indeed produce a form of polymorphism with interfaces, not just straight inheritance.
![]() ![]() | ||