Polymorphism

Polymorphism is the ability to change the implementation of a base class for different objects. For example, if you have a bicycle and a car, both can move, but they do so in very different ways. They use different mechanisms for movement, and the distance that each can move in an hour is significantly different. Yet, both a Car and a Bike class might inherit from a base Transportation class, which could also be used as the basis for a Plane class, a Train class, a HotAirBalloon class, and so on.

Polymorphism with Inheritance

Polymorphism was possible in VB6 using interfaces, which will be examined in a moment. VB .NET allows you to perform polymorphism using inheritance. The difference from what you have done so far is that you can actually use the base class as a variable type, and you can handle any derived class with that new variable.

For example, examine the following code. The Transportation class contains just one method, Move. Two classes inherit the Transportation class: Car and Bicycle. Both have overridden the Move method. The code looks like this:

Public MustInherit Class Transportation
    Public MustOverride Function Move() As Boolean
End Class

Public Class Bicycle
    Inherits Transportation
    Overrides Function Move() As Boolean
        'code here
        Move = True
    End Function
End Class

Public Class Car
    Inherits Transportation
    Overrides Function Move() As Boolean
        'different code here
        Move = True
    End Function
End Class

So far, this looks similar. However, notice now what your client can do. It cannot directly create an instance of Transportation because it is marked as MustInherit. But, you can declare a variable of type Transportation. You can be assured that any object that inherits Transportation has a Move method, and you don't have to worry about what kind of object it is. Your client code might look like this:

Protected Sub Button1_Click _
  (ByVal sender As Object, ByVal e As System.EventArgs)
    Dim MyCar As New Car()
    Dim MyBike As New Bicycle()

    PerformMovement(MyCar)
    PerformMovement(MyBike)
End Sub

Public Sub PerformMovement(ByVal Vehicle As Transportation)
    If Vehicle.Move() Then
        'do something
    End If
End Sub

You'll notice that the PerformMovement Sub accepts an argument of type Transportation. This Sub doesn't care if you pass it an object of Car or Bicycle type. Because the object being passed in inherits from Transportation, it is guaranteed to support the Move method, so the code will run without problems.

Polymorphism with Interfaces

Interfaces still exist in VB .NET. In VB6 and COM, they were most often used when you needed to be able to modify your code without breaking existing clients. You could actually modify the structure of your classes, but provide an interface that looked like the old version of the component. This kept existing client applications happy while at the same time you were able to modify the classes to enhance functionality.

In VB .NET, you can still use interfaces this way. However, interfaces are best used when you want to be able to use different object types in the same way, and the objects don't necessarily share the same base type. For example, the IEnumerable interface is used to expose an enumerator for a class. Using this interface, you can move through a class using For Each, even if the class is not based on a Collection object.

You'll see an example of polymorphism with interfaces using the same example of the Car and the Bicycle. First, Transportation will be created as an interface instead of a base class, which might make more sense: The Move method is most likely to be quite different between a car and bicycle. Then, you'll implement the Transportation interface. Finally, you'll call the objects from the client.

Interface Transportation
    Function Move() As Boolean
End Interface

Public Class Bicycle
    Implements Transportation
    Function Move() As Boolean Implements Transportation.Move
        'code here
        Move = True
    End Function
End Class

Public Class Car
    Implements Transportation
    Function Move() As Boolean Implements Transportation.Move
        ' different code here
        Move = True
    End Function
End Class

You'll notice that now, when you implement from an interface, you don't have to use the Overrides modifier. In addition, the method definition is followed by an Implements keyword that specifies which method in the interface the current method is implementing. This allows you to have different names for the methods in the class that is implementing the interface. The client code here will be the same as it is when you use inheritance polymorphism.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.18.4