In software programming, object-oriented programming (OOP) is a framework that attempts to create objects out of real-world things. The theory and practice of polymorphism is strongly encouraged within OOP principles. In general, an object should be created to have base features and behaviors with the understanding that these will change when it becomes a specific type.
Polymorphism is the ability to have a variable, function, or an object with more than one meaning within the program. There are multiple architecture designs that demonstrate polymorphism. These designs outline how an object will be reused within an application for multiple purposes.
Many programming languages support the use of polymorphism. This process enables the reuse of business rules and software code throughout an application. Not all programming languages support full polymorphic behavior but most support the basic concept. All of the true OOP languages including Java®, .net, and C++® support more advanced approaches and techniques.
A bank account is a good example of base object that could support polymorphism. All bank accounts have account numbers, names, and a balance. What makes an account unique is the type of account. Some examples of types are savings accounts, checking accounts, or a money market accounts. Within polymorphism, a bank account would be the base object with the more specific accounts using the features of the base bank account. Each account could then have additional behaviors to support interest rates, or withdrawal penalties while reusing the information about a bank account.
Using the theories of polymorphism saves developers time by reducing wasted code. It makes code easier to write and easier for others to understand. Additionally, it makes the software extensible, because future types can be added later using the base ancestor object within the existing code. In general, applications developed in this manner are more flexible and easier to extend by requiring less code for future modifications
The theories of polymorphism also apply to functions. A function is a piece of software code that performs a specific task. Functions can also be written in a polymorphic manner. This approach makes the code more flexible because the functions can be reused for other business rules within the software.
A good example of polymorphism with a function would be a sort function. This type of function would sort a list of numbers. A polymorphic function could not only sort numbers but could also sort any type of objects. This makes the function more efficient because it works on multiple types of data.