Browse Definitions :
Definition

polymorphism

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.
diagram illustrating how object-oriented programming works
Polymorphism, the idea that entities in code can have more than one form, is a popular concept in object-oriented programming.

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

Networking
  • firewall as a service (FWaaS)

    Firewall as a service (FWaaS), also known as a cloud firewall, is a service that provides cloud-based network traffic analysis ...

  • private 5G

    Private 5G is a wireless network technology that delivers 5G cellular connectivity for private network use cases.

  • NFVi (network functions virtualization infrastructure)

    NFVi (network functions virtualization infrastructure) encompasses all of the networking hardware and software needed to support ...

Security
  • Advanced Encryption Standard (AES)

    The Advanced Encryption Standard (AES) is a symmetric block cipher chosen by the U.S. government to protect classified ...

  • operational risk

    Operational risk is the risk of losses caused by flawed or failed processes, policies, systems or events that disrupt business ...

  • risk reporting

    Risk reporting is a method of identifying risks tied to or potentially impacting an organization's business processes.

CIO
  • Risk Management Framework (RMF)

    The Risk Management Framework (RMF) is a template and guideline used by companies to identify, eliminate and minimize risks.

  • robotic process automation (RPA)

    Robotic process automation (RPA) is a technology that mimics the way humans interact with software to perform high-volume, ...

  • spatial computing

    Spatial computing broadly characterizes the processes and tools used to capture, process and interact with three-dimensional (3D)...

HRSoftware
  • OKRs (Objectives and Key Results)

    OKRs (Objectives and Key Results) encourage companies to set, communicate and monitor organizational goals and results in an ...

  • cognitive diversity

    Cognitive diversity is the inclusion of people who have different styles of problem-solving and can offer unique perspectives ...

  • reference checking software

    Reference checking software is programming that automates the process of contacting and questioning the references of job ...

Customer Experience
  • martech (marketing technology)

    Martech (marketing technology) refers to the integration of software tools, platforms, and applications designed to streamline ...

  • transactional marketing

    Transactional marketing is a business strategy that focuses on single, point-of-sale transactions.

  • customer profiling

    Customer profiling is the detailed and systematic process of constructing a clear portrait of a company's ideal customer by ...

Close