What is polymorphism?
Polymorphism is a popular concept in object-oriented programming (OOP), referring to the idea that an entity in code such as a variable, function or object can have more than one form. The word polymorphism is derived from Greek and means "having multiple forms." Apart from computer programming, the idea of polymorphism occurs in other real-world areas, including biology, chemistry and drug development.
Polymorphism is one of the most important concepts in OOP. It describes the ability of something to have or to be displayed in more than one form. The different forms arise because these entities can be assigned different meanings and used in various ways in multiple contexts.
Many programming languages display or allow polymorphism, including Java, Ruby, C++, PHP and Python. In these languages, polymorphism enables class objects belonging to the same hierarchical tree to behave differently, even though they might have functions with the same name. In PHP, for example, polymorphism means that if B is a descendant of A and a function can accept A as a parameter, then it can also accept B as a parameter.
Variables and named functions in polymorphism and OOP
In programs supporting polymorphism, a named function can also vary depending on the parameters it is given. For example, if given a variable that is an integer, the function chosen would seek a match against a list of employee numbers. If the variable is a string of text characters, it would seek a match against a list of names. In either case, both functions would be known in the program by the same name.
For example, a variable named USERID might be capable of being either an integer (a whole number) or a string of characters. Both choices might be supported because the program lets a user enter, for example, a user ID. This ID could be a number or a name. Polymorphism gives the program a way to distinguish between the two forms of input, so either kind can be recognized and processed.
Types of polymorphism
In computer programming, there are two main types of polymorphism:
- Compile time polymorphism. Also known as static polymorphism, compile time polymorphism is common in OOP languages like Java. It uses method overloading to create multiple methods that have the same name in the same class, but a different number of parameters. They may also have parameters for different data types.
- Runtime polymorphism. Also known as dynamic polymorphism, runtime polymorphism uses method overriding to let a child class have its own definition of a method belonging to the parent class. This type of polymorphism is often associated with upcasting, which happens when a parent class points to an instance of the child’s class.
Polymorphism can also be implemented in other ways in OOP, including:
- Ad-hoc polymorphism. A function is applied to different arguments and might operate differently depending on the argument type.
- Parametric polymorphism. Programmers can write generic functions that treat values in a data set the same way, regardless of their type.
- Subtyping polymorphism. Programmers can create multiple functions or subroutines that can operate on the supertype class and on various subtypes.
- Row polymorphism. Programmers can write programs that operate only on a specified section of a database record, i.e., a row.
- Polytypism. Programmers use this to create algorithms in which the types can be provided as parameters when the algorithm is instantiated.
Polymorphism in C++
C++ programming provides a good example of polymorphism. In this language, the plus sign (+) is an operator and a simple named function. It can be assigned to operate on two objects, adding them together, and it also can perform Boolean searching. In the latter case, the + sign can indicate a logical "and."
Adding objects is one of the most common forms of the + sign operation. In Boolean searching, the words separated by the + operator must be present for a result such as a citation to be returned. In yet another context, the + sign can indicate an operation to concatenate or link the two objects or strings of letters.
Further, a given operator can be combined with another operator to give a different meaning. In C++, a + operator can be combined with another + operator to create the ++ operator. When ++ follows a variable, it can mean "increment (increase) this value by 1."
When all is said and done, the meaning of an operator is defined as part of a class definition in the language. Because a programmer can create classes in these languages, they can also define how operators will work for a class of objects. In effect, they can redefine the language and program for their specific purpose or goals. This becomes possible due to polymorphism.
Importance of polymorphism in OOP
Polymorphism lets programmers perform a single action in various ways and define multiple forms of a single object, variable or method. It also improves the readability and scalability of code since old code can be reused and built upon as required after it is written, confirmed and executed. Reusing code also saves significant programming time and helps to speed up development.
Another advantage of polymorphism is that it simplifies variable searches and execution, as well as code debugging. Polymorphism is particularly useful when a program requires the use of a single method name to automatically invoke the correct method depending on the class. Also, it lets programmers replace conditional statements in code by using switch statements designed to determine which code to run when it is called, depending on the first parameter received.
Other application areas of polymorphism
Apart from computer programming, polymorphism exists in other real-world areas.
In machine learning, polymorphism can mean a data type of "any." As a result, a list can be processed by a function, regardless of which data types it contains. For example, if a model includes a function that simply determines the length of a list, it doesn't matter what data types are in the list. The list will still be processed and deliver an output.
In biology and genetic studies, polymorphism refers to situations where two or more possibilities of a trait are found in one gene. For example, jaguars and cheetahs have dual-tone skins because they have different morphs or traits for skin color. Since these animal species have more than one form of a trait, they are said to exhibit polymorphism.
Chemistry and materials science
Polymorphism is also observed in chemistry and materials science, and it has important implications for numerous areas, including agrochemicals, medicines, foods, pigments and even explosives.
In all these areas, polymorphism refers to the idea that a material or chemical substance – an element or compound -- can exist in more than one crystalline form. For example, carbon is one of the most well-known polymorphs. It can exist as both graphite and as a diamond, making it valuable for manufacturing a wide range of items, such as pencils, batteries, the cores of nuclear reactors and jewelry.
Polymorphism is also a common and important concept in pharmaceuticals and drug development. It enables pharma companies to develop a polymorph of a medicine that is more effective and safer for human consumption than its other polymorphs.
It is not uncommon for a drug to receive a patent and/or regulatory approval for one of its polymorphs but not for others. The analgesic acetaminophen, the anti-inflammatory drug aspirin, the cholesterol-lowering drug atorvastatin, and the HIV treatment drug ritonavir are all examples of drugs that exist in polymorphic form.