Evaluate your SIEM
Get the guideComplete visibility for DevSecOps
Reduce downtime and move from reactive to proactive monitoring.
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.
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.
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 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 record section.
Polytypism
Polytypism, or data type genericity, is used in generic polymorphism programming. In generic programming, programmers create algorithms to specify the types later. The required types can be provided as parameters when the algorithm is instantiated later. A generic type is also known as a parameterized type.
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 more well-suited to detecting polymorphic malware than traditional signature-based detection methods.
Examples of polymorphism in Java include method overriding, where a subclass provides a specific implementation of a method already defined in its superclass, and method overloading, where methods within the same class have the same name but different parameters. In Python, polymorphism can be seen in function overloading, where functions of the same name can behave differently based on the number or types of parameters, and operator overloading, allowing operators to behave differently based on the operands they work with.
Polymorphism in data analytics and data science offers significant advantages by enabling the creation of flexible and reusable code that can adapt to various data types and structures. More efficient handling of diverse datasets enhances the scalability and modularity of data analysis processes. By leveraging polymorphism, data analysts can write generic algorithms that operate on different data types without needing explicit type checking, streamlining code maintenance and promoting code reusability. Additionally, polymorphism facilitates the implementation of inheritance hierarchies, promoting code organization and facilitating the management of complex data structures commonly encountered in data science tasks.
Inheritance in object-oriented programming allows a new class to inherit attributes and methods from an existing class. This concept is significant because it promotes code reusability, reduces redundancy, and facilitates the creation of a hierarchy among classes. Inheritance enables the formation of parent-child relationships, allowing child classes to extend or override the behavior of their parent classes. This results in a more organized and efficient code structure, making it easier to maintain and scale software projects.
Reduce downtime and move from reactive to proactive monitoring.