What is polymorphism? | Definition from TechTarget (2024)

What is polymorphism? | Definition from TechTarget (1)

By

  • Rahul Awati

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

When polymorphism is supported, a variable with a given name may be allowed to have different forms. The program can determine which form of the variable to use at the time of execution.

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.
What is polymorphism? | Definition from TechTarget (2)

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.

Machine learning

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.

Biology

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.

Pharmaceuticals

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.

Check out this breakdown of object-oriented programming concepts.

This was last updated in June 2023

Continue Reading About polymorphism

  • Functional vs. object-oriented programming: The basics
  • A comparison of 6 top programming languages
  • Choose the best programming language for DevOps workflows
  • Why hackers should learn Python for pen testing
  • How to learn Golang: A DevOps engineer's experience

Related Terms

What is a bitwise operator?
A bitwise operator is a character that represents an action taken on data at the bit level, as opposed to bytes or larger units ...Seecompletedefinition
What is an API endpoint?
An API endpoint is a point at which an application programming interface -- the code that enables two software programs to ...Seecompletedefinition
What is computer-aided software engineering (CASE)?
Computer-aided software engineering (CASE) describes a broad set of labor-saving tools and methods used in software development ...Seecompletedefinition
What is polymorphism? | Definition from TechTarget (2024)
Top Articles
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5629

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.