Polymorphism in VB.NET
Polymorphism is the concept that different objects have different implementations of the same characteristic. For example, consider two objects, one representing a Porsche 911 and the other a Toyota Corolla. They are both cars; that is, they both derive from the Car class, and they both have a drive method, but the implementations of the methods could be drastically different.
Polymorphism sounds a great deal like encapsulation, but it is different. Encapsulation has to do with hiding the internal implementation of an object. Polymorphism has to do with multiple classes having the same interface.
Visual Basic introduced a very limited support for polymorphism in VB 4.0 through the use of late binding. Late binding is the technology for code to determine at runtime what properties and methods a given object provides. This allows code to create any object and attempt to call a method or property of that object without knowing whether the object supports the method at compile time. Given two different classes with the same properties and methods, a variable of type object could be used to instantiate an object of either class. This worked, but did not provide any type safety at compile time. Type safety is the concept to ensure that a string is used as a string, an integer is used as an integer, and a Boolean is used as a Boolean. Without type safety, potentially more errors can occur when a user is executing a program rather than when the program is compiled. Type safety also is one mechanism to prevent hackers from overloading code with a destructive piece of code.
VB 5 introduced an additional form of polymorphism using interfaces. An interface defines a set of properties, methods, and events that a class implements. Any number of classes can implement the same interface. A variable that instantiates an interface can reference any class that implements that interface.
Interfaces provide early binding to an object, which can greatly improve performance and also provide type protection at compile time. Interfaces were the most important feature introduced in VB 5, but are still one of the least understood or used.
As you'll see later in this chapter, VB.NET has improved support for interfaces. The language now has direct support for interfaces creation, which was done in earlier versions either via IDL or by creating empty classes. In addition, a single method can be the implementation of methods in multiple interfaces.
Listing 3.7 illustrates polymorphism through late binding and Listing 3.8 illustrates polymorphism through early binding. Both listings show classes that implement Ride methods. As shown, whether using either late binding or early binding, a method can call either class's method without caring what kind of object it is using. This is the essence of polymorphism: It provides the ability to create code that can use different objects that implement the same interfaces.
Listing 3.7 Polymorphism Through Late Binding
Public Class RollerCoaster Public Sub Ride() Console.WriteLine("Here we go") Console.WriteLine("Click, Click ,Click") Console.WriteLine("Oh, *&@&#%") Console.WriteLine("That was great") End Sub End Class Public Class MerryGoRound Public Sub Ride() Console.WriteLine("OK will go on it") Console.Writeline("Nap Time") Console.WriteLine("Yea its over") End Sub End Class
Some code somewhere else in the application:
Private Sub DayAtTheAmusementPark() Dim oRollerCoaster as New RollerCoaster Dim oMerryGoRound as New MerryGoRound Call GoOnRide(oRollerCoaster) Call GoOnRide(oMerryGoRound) End Sub Private Sub GoOnRide(oRide as Object) oRide.Ride() End Sub
For better performance and to find errors at compile time, early binding is the preferred choice. Early binding can be accomplished through the use of Interfaces (see _Listing 3.8).
Listing 3.8 Polymorphism Through Interfaces
Public Interface IAmusementParkRide Sub Ride() End Interface Public Class RollerCoaster Implements IAmusementParkRide Public Sub IAmusementParkRide_Ride() Console.WriteLine("Here we go") Console.WriteLine("Click, Click ,Click") Console.WriteLine("Oh, *&@&#%") Console.WriteLine("That was great") End Sub End Class Public Class MerryGoRound Implements IAmusementParkRide Public Sub IAmusementParkRide_Ride() Console.WriteLine("OK will go on it") Console.Writeline("Nap Time") Console.WriteLine("Yea its over") End Sub End Class
Some code somewhere else in the application:
Private Sub DayAtTheAmusementPark() Dim oRollerCoaster as New RollerCoaster Dim oMerryGoRound as New MerryGoRound Call GoOnRide(oRollerCoaster) Call GoOnRide(oMerryGoRound) End Sub Private Sub GoOnRide(oRide as IAmusementParkRide) oRide.Ride() End Sub