What is Polymorphism?
The term polymorphism is separately defined in four scientific disciplines: biology, genetics, biochemistry, and computer science. In the biological context, polymorphism refers to the idea that members of the same population or colony can take on different visual characteristics. In genetics, polymorphism reflects variation in the genetic coding of individuals in a population. In the field of biochemistry, the term polymorphism is used to describe subtle structural differences in proteins that are otherwise identical.
Looking at the greek roots of the term polymorphism can help to further clarify the similarities between these definitions. The word poly means "many" and the word morph means "form", so when we talk about a polymorphism we're talking about something that appears in many different forms.
This brings us to our computer science definition of polymorphism - we'll offer three different versions for maximum clarity.
- Polymorphism is a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times
- Polymorphism is the ability of a programming language to present the same interface for several different underlying data types
- Polymorphism is the ability of different objects to respond in a unique way to the same message
A Simple Example of Polymorphism
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. Without getting too deep into specifics of how to write polymorphic code (given that it can be done differently depending on which language you use), let's review two of the most common examples used to illustrate polymorphism in computer science.
Polymorphism 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 code polymorphism, 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, radius for circles.
Now our base class shape has two derived classes - rectangle and circle - each with its own 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 is known as method overloading.
What are the Five Types of Polymorphism?
There are many different ways to implement polymorphism to solve problems in computer science and programming. Below, we briefly describe five different types of polymorphism that are commonly used in various programming languages.
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 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.
In the example above, we defined a class called shapes and derived classes called circle and rectangle. In subtyping, we define a supertype that may have several subtypes where various functions or subroutines were written to operate on the supertype 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 Dog.
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, also known as data type genericity, is used in generic 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.