Pricing Login
Pricing

DevOps and Security Glossary Terms

Glossary Terms
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Polymorphism - definition & overview

In this article
What is polymorphism?
Types of polymorphism: example and problems
What are the five types of polymorphism?
Sumo Logic guards against polymorphic threats
What is polymorphism?
Types of polymorphism: example and problems
What are the five types of polymorphism?
Sumo Logic guards against polymorphic threats

What is polymorphism?

Polymorphism is a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times. It is the ability of a programming language to present the same interface for several different underlying data types and different objects to respond in a unique way to the same message.

Key takeaways

  • Polymorphism is a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times.
  • Polymorphism in programming gives a program the ability to redefine methods for derived classes.
  • Five different types of polymorphism that are commonly used in various programming languages are Ad-hoc, Parametric, Subtyping, Row, and Polytypism.
  • Sumo Logic helps enterprise organizations detect malware and other unwanted intrusions with behavior-based threat detection techniques that are more well-suited to detecting polymorphic malware than traditional signature-based detection methods.

Types of polymorphism: example and problems

Polymorphism is one of the core characteristics of any object-oriented programming language. Languages such as Ruby, Java, C++, and Python all support polymorphism. Polymorphic code allows a program to process objects differently depending on their data type or class, with the ability to redefine methods for derived classes. Here are some of the most common examples used to illustrate polymorphism in computer science.

Polymorphism shape examples

Polymorphism in programming gives a program the ability to redefine methods for derived classes. Imagine that we write a piece of code where we define a base class called shape.

We may want to define specific methods such as area and perimeter that can be applied to our base class - but there's a problem. Our knowledge of geometry tells us that we cannot use the same method to compute the area and perimeter of both a circle and a rectangle. In particular, the area and perimeter of a circle depend on its radius and the area and perimeter of a rectangle depending on its width and length.

To solve this problem using polymorphism coding, we could define a circle and rectangle as sub-classes or derived classes of the base class shape. Each sub-class can have its own method for area and perimeter and each method will accept different parameters - length and width for rectangles, and radius for circles.

Now our base class shape has two derived classes - rectangle and circle - each with its methods for area and perimeter. When we call one of the methods, the provided set of parameters can be used to determine which method should be called. If we provide a single parameter for the radius, our program can call the methods that belong to the class circle. If we provide two parameters for length and width, our program can call the methods that belong to the class rectangle. This represents a type of static polymorphism known as method overloading.

What are the five types of polymorphism?

There are many different ways to implement programming polymorphism to solve problems in computer science. Below, we briefly describe five different types of polymorphism that are commonly used in various programming languages.

Ad-hoc polymorphism in computer science

Ad-hoc polymorphism refers to the idea that a specific function can be applied to different arguments and may operate differently depending on the type of arguments to which it is applied. A basic example of a polymorphic function is the plus sign (+) operator. This operator can be used in several ways, including integer addition, floating-point addition, list concatenation, and string concatenation. When a single operator performs different functions depending on the type of argument, we say that the operator is overloaded. Suitably, ad-hoc polymorphism is also called function overloading or operator overloading.

Parametric polymorphism

Parametric polymorphism allows programmers to write generic functions that can treat values the same way regardless of their type. Put differently, a parametric function is a piece of code that treats all data the same way, regardless of its type. Parametric polymorphism is used to write generic functions that operate on data sets, regardless of the specific type of each element in the set.

Subtyping polymorphism in object-oriented programming

In the example above, we defined a class called shapes and derived classes called circles and rectangles. In subtyping, we define a supertype that may have several subtypes where various functions or subroutines were written to operate on the supertype and can also operate on the various subtypes.

We might define the supertype "Dogs" and subtypes like "Golden Retrievers," "Basset Hounds" and "Chihuahuas." Each subtype is a variety of the supertype that shares some, but not all of its characteristics. Subtype polymorphism allows a function that would normally operate on a dog-type object to also operate on objects that belong to any of the subtypes of Dogs. You can write a method that takes a Dog as a parameter, but you can also use Basset Hounds as a parameter because a Basset Hound is a subtype of a Dog.

Row polymorphism

Row polymorphism is a special type of polymorphism that deals with records in databases or spreadsheets, commonly referred to as rows. Row polymorphism can be used to write programs that operate solely on a specified section of a record.

Polytypism

Polytypism, also known as data type genericity, is used in generic polymorphism programming. In generic programming, programmers create algorithms where the types can be specified at a later time. When the algorithm is instantiated at a later point, the required types can be provided as parameters. A generic type is also known as a parameterized type.

Sumo Logic guards against polymorphic threats

Cyber attackers are using polymorphism to create malicious code and scripts that change the form, including changing their filename, encryption and compression characteristics. These programs are increasingly difficult to detect with anti-malware programs. Researchers at Webroot recently discovered that 97% of malware infections leverage polymorphism to circumvent security measures.

Sumo Logic helps enterprise organizations detect malware and other unwanted intrusions with behavior-based threat detection techniques that are more well-suited to detecting polymorphic malware than traditional signature-based detection methods.

Complete visibility for DevSecOps

Reduce downtime and move from reactive to proactive monitoring.