Ad hoc polymorphism is a programming concept used to describe functions with the same name that are executed differently, depending on the variable or argument type. It is also referred to as “function overloading” or “method overloading.” The “sum” function is an example of this. When you execute it on “3 + 5” (integers), the result will be 8.
However, if the variables are changed into text strings, the “sum” function will concatenate or link the two strings together. For instance, “ad” + “hoc” will become “ad hoc.” Both operations used the same function, but the execution and results differ.
Other interesting terms…
Read More about an “Ad Hoc Polymorphism”
In programming, ad hoc polymorphism is only one type of polymorphism. Understanding the concept requires a basic idea of what polymorphism is.
What Is Polymorphism?
Polymorphism is a programming theory that allows various objects to respond to the same message differently. In other words, objects are processed based on their data type. A real-life example of polymorphism is when you go to the Amazon website.
If you set your location to the U.S., the products you will see can be shipped to the country. If you change your address, the product listing will also change based on your chosen country.
The term is derived from a biology principle where organisms can have multiple forms of a trait. An example is the presence of dark and light spots in jaguars.
What Are Some Examples of Ad Hoc Polymorphism?
Now that we’ve established a basic understanding of polymorphism, we can zoom in on one of its types—ad hoc polymorphism. In this programming concept, a single method or function can have different implementations, depending on the type.
We mentioned the “sum” function as an example. Another example of ad hoc polymorphism is when you print something. The print function is implemented differently, depending on what you’re printing. If you’re printing text, the program would need to set letter and paragraph spacing, fonts size and style, and other settings. These aren’t required when printing images.
Sort can also be considered a form of ad hoc polymorphism since you can apply it to different types of integers, names, dates, and other variables.
What Is the Difference between Ad Hoc and Universal Polymorphism?
Universal or parametric polymorphism is another type of polymorphism. Unlike ad hoc, which is based on type, universal polymorphism is type-agnostic. Ad hoc polymorphism is derived from the loose translation of “ad hoc,” which is “for this.” That means the polymorphism relates specifically to certain data types.
In universal polymorphism, functions and methods are implemented uniformly, regardless of data type. Remember that we cited the “sort” function as an example of ad hoc polymorphism. That is true when sorting lists containing only one data type, such as students’ names.
However, the “sort” function can also be written generically, applying the universal polymorphism concept. You use a generic sorting method when the list to sort includes a combination of numbers and alphabet characters. In this case, the “sort” function won’t care about the types of data on the list.
Why Is Ad Hoc Polymorphism Also Called “Overloading”?
Another term for ad hoc polymorphism is overloading since you can apply the same function to different variable types. Overloading means that programmers can call the same function or method for different data types.
While the word connotes an unpleasant meaning, overloading benefits programmers since they don’t have to come up with or memorize several function names. They can use the same name or symbol even if the implementations differ.
Imagine finding another name for the “sum” function when executing it on text strings. Overloading saves time and makes the program reusable and more readable.
What Programming Languages Allow Ad Hoc Polymorphism?
Among the programming languages that support ad hoc polymorphism are:
- PostgreSQL and PL/SQL
- Wolfram Language
It may sound like a complicated term, but the concept behind ad hoc polymorphism is quite simple. If you think about it, we all exhibit this characteristic. People have one name but they may act differently, depending on their environment. A person can be a loving and playful parent or sibling at home. At work, that same person needs to act professionally.