Polymorphism is often considered to be directly tied to inheritance. In reality, it is independent. Polymorphism in software engineering means that you can have two classes with different implementations or code, but with a common set of methods, properties, or events. You can then write a program that operates upon that interface and does not care about which type of object it operates at runtime.
To properly understand polymorphism, you need to explore the concept of a method signature, sometimes also called a prototype. All methods have a signature, which is defined by the method's name and the data types of its parameters. You might have code such as this:
Public Function CalculateValue() As Integer End Sub
In this example, the signature is as follows:
If you add a parameter to the method, the signature will change. For example, you could change the method to accept a Double:
Public Function CalculateValue(ByVal value As Double) As Integer
Then, the signature of the method is as follows:
Polymorphism merely says that you should be able to write client code that calls methods on an object, and as long as the object provides your methods with the method signatures you expect, it doesn't matter from which class the object was created. The following sections look at some examples of polymorphism within Visual Basic.
You can use several techniques to achieve polymorphic behavior:
- Multiple interfaces ...