What is Polymorphism?

Troy Holmes

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.

All of the true OOP languages, including C++, support more advanced approaches and techniques of polymorphic behavior.
All of the true OOP languages, including C++, support more advanced approaches and techniques of polymorphic behavior.

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.

Get started

Want to automatically save money while you shop online?

Join 3 million Wikibuy users who have found 
$70 million in savings over the last year.

Wikibuy compensates us when you install Wikibuy using the links we provided.

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.

You might also Like

Discussion Comments

Lostnfound

@Grivusnagel -- I nearly spit my coffee out when I read your post. Too funny! I think that describes a lot of us who have to depend on computers and mainframes to do our work.

You actually (probably unintentionally) hit the nail on the head in your comment. For polymorphism to work in the technology environment, every polymorphic element has to do every one of its several jobs equally well. Some programs are better at it than others.

Our front end publishing program, according to the IT guy who actually knows something, is slow because it gets too hung up in archiving files, even though that's not what you're asking it to do. You can be moving a file, but it wants to archive everything, so it slows the system way down. It's a quirk of this particular system. Other systems don't have these issues.

Grivusangel

This explains a lot about our computer system at work. It does too many things, and none of them very well. Now I understand.

Post your comments
Login:
Forgot password?
Register: