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
  • subnet (subnetwork)

    A subnet, or subnetwork, is a segmented piece of a larger network. More specifically, subnets are a logical partition of an IP ...

  • Transmission Control Protocol (TCP)

    Transmission Control Protocol (TCP) is a standard protocol on the internet that ensures the reliable transmission of data between...

  • secure access service edge (SASE)

    Secure access service edge (SASE), pronounced sassy, is a cloud architecture model that bundles together network and cloud-native...

Security
  • cyber attack

    A cyber attack is any malicious attempt to gain unauthorized access to a computer, computing system or computer network with the ...

  • digital signature

    A digital signature is a mathematical technique used to validate the authenticity and integrity of a digital document, message or...

  • What is security information and event management (SIEM)?

    Security information and event management (SIEM) is an approach to security management that combines security information ...

CIO
  • product development (new product development)

    Product development -- also called new product management -- is a series of steps that includes the conceptualization, design, ...

  • innovation culture

    Innovation culture is the work environment that leaders cultivate to nurture unorthodox thinking and its application.

  • technology addiction

    Technology addiction is an impulse control disorder that involves the obsessive use of mobile devices, the internet or video ...

HRSoftware
  • organizational network analysis (ONA)

    Organizational network analysis (ONA) is a quantitative method for modeling and analyzing how communications, information, ...

  • HireVue

    HireVue is an enterprise video interviewing technology provider of a platform that lets recruiters and hiring managers screen ...

  • Human Resource Certification Institute (HRCI)

    Human Resource Certification Institute (HRCI) is a U.S.-based credentialing organization offering certifications to HR ...

Customer Experience
  • contact center agent (call center agent)

    A contact center agent is a person who handles incoming or outgoing customer communications for an organization.

  • contact center management

    Contact center management is the process of overseeing contact center operations with the goal of providing an outstanding ...

  • digital marketing

    Digital marketing is the promotion and marketing of goods and services to consumers through digital channels and electronic ...

Close