oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Object Oriented Programming for VB.NET - Part 2
Pages: 1, 2, 3


Polymorphism is probably the hardest concept to digest for those new to OOP. Indeed, polymorphism (literally means "many forms") is probably the most misunderstood term in OOP because the word has more than one meaning. For example, if you type in the word polymorphism as a search keyword in an Internet search engine, you will find out that this word is also used in biology.

In modern software engineering discipline, polymorphism is classified into universal polymorphism and ad-hoc polymorphism. Universal polymorphism is considered purer than ad-hoc polymorphism and is also called "true polymorphism." Ad-hoc polymorphism is often described as "apparent polymorphism." Universal polymorphism is subdivided into parametric polymorphism and inclusion polymorphism; and ad-hoc polymorphism can be subdivided into overloading and coercion. These polysyllabic forms of polymorphism are explained below.

  • Parametric polymorphism is the purest form of polymorphism*. The same object or function can be used uniformly as a parameter in different type contexts without changes. A function can be thought as parametric polymorphic if it allows the type of its parameters to be determined by an implicit or explicit type parameter, and then performs the same kind of work irrespective of the parameter type.
  • Inclusion polymorphism is what we know as subtyping and inheritance. This type of polymorphism allows an object to belong to a number of classes, which are sometimes overlapping. In other words, a class can be included in another class to create an inheritance hierarchy.
  • Overloading is what is known as method overloading in object-oriented programming languages. It allows multiple functions to have the same name, yet still perform different functions.
  • Coercion allows a parameter to a function to be converted to the type expected by that function to avoid a type error.

This classification was put forward by Luca Cardelli (AT&T Bell Laboratories) and Peter Wegner (Brown University) in their paper, "On Understanding Types, Data Abstraction and Polymorphism" (PDF), published in Computing Surveys in 1985. This is the presently accepted definition of polymorphism. The older definition, first put forth by C. Strachey in 1967, identified only two types of polymorphism: parametric polymorphism and ad-hoc polymorphism.

In OOP, when we talk about polymorphism, it simply means "one interface, many implementations." To be more specific, the term is used to describe reference variables that may at run-time refer to objects of different classes. (Overloading is also a type of polymorphism; however, in OOP it is simply referred to as method overloading.) In other words, polymorphism (as far as OOP is concerned) simply means that you can assign to an object reference an object whose type is different from the object reference. In essence, if you have an object reference a whose type is A, it is legal to assign and object of type B, as in the following.

Dim a As A
a = New B()

However, B must be derived from A either directly or indirectly, or A must be an interface that is implemented by B or the class that B extends directly or indirectly.

Consider the example in Listing 24.

Listing 24: An example of polymorphism

Class Employee
  Public Overridable Sub Work
    System.Console.WriteLine( "I am an employee." )
  End Sub
End Class

Class Manager : Inherits Employee
  Public Sub Manage
    System.Console.WriteLine("Managing ...")
  End Sub
  Public Overrides Sub Work
    System.Console.WriteLine( "I am a manager" )
  End Sub
End Class

Module ModMain
  Sub Main()
    Dim employee As Employee
    employee = New Manager
  End Sub
End Module

The example has two classes: Employee and Manager. Employee has a method called Sub and Manager extends the class Employee and adds a new method called Manage. The Main sub in the module defines an object variable called employee of type Employee.

Dim employee As Employee

However, employee is assigned an object of type Manager, as in

employee = New Manager

Then, when the Work method is called, guess what is written on the console? I am a manager. This means that it is the Work method of Manager class that gets called. Polymorphism in action!

But why do we declare employee as Employee in the first place? Why don't we declare employee as Manager? To ensure flexibility in cases where we don't know whether the object reference (employee) will be assigned an object of type Manager or something else. The next example will make it clearer why we use polymorphism.

Say you have a Windows application and you have a method that changes the BackColor property of a control. You want to be able to pass a Button, a Label or any control that may be used in a form. For this purpose, you write the subroutine in Listing 25.

Listing 25: The ChangeColor subroutine

Private Sub ChangeColor(_
  ByVal control As System.Windows.Forms.Control, _
  ByVal color As system.Drawing.Color)
  control.BackColor = color
End Sub

You pass two arguments to the ChangeColor sub: a System.Windows.Forms.Control object and a System.Drawing.Color object. You know that the Control class is the parent class of all Windows controls. By declaring control as Control, polymorphism allows you to pass a Control object or any object whose class is derived from the Control class. This way, you only need one sub for all controls. Without polymorphism, you need a sub for each type of control. You would have ChangeButtonColor, ChangeLabelColor, etc

The code in Listing 26 iterates all controls in a Windows form and passes each control to the ChangeColor sub. Running this code will give all controls in the current window a blue background color.

Listing 26: Passing different controls to the ChangeColor subroutine

Dim control As System.Windows.Forms.Control
For Each control In Me.Controls
  ChangeColor(control, Color.Blue)


By now, you should have a much better understanding of all those cool words I presented at the beginning of the article. With this understanding, you can start to take advantage of VB.NET's complete support for object-orienting programming. Happy OOPing!

Return to the .NET DevCenter.