Polymorphism
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.
Method Signatures
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:
f()
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:
f(Double)
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.
Implementing Polymorphism
You can use several techniques to achieve polymorphic behavior:
- Inheritance
- Multiple interfaces ...
Get Professional Visual Basic 2012 and .NET 4.5 Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.