Polymorphism

KS3 Computer Science

11-14 Years Old

48 modules covering EVERY Computer Science topic needed for KS3 level.

GCSE Computer Science

14-16 Years Old

45 modules covering EVERY Computer Science topic needed for GCSE level.

A-Level Computer Science

16-18 Years Old

66 modules covering EVERY Computer Science topic needed for A-Level.

GCSE Programming Resources (14-16 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

A-Level Problem solving and programming (16-18 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

What is Polymorphism

The term polymorphism is independently characterised in four logical orders: science, hereditary qualities, natural chemistry, and software engineering. In the organic setting, polymorphism alludes to the possibility that individuals from a similar populace or state can take on various visual qualities. In hereditary qualities, polymorphism reflects variety in the hereditary coding of people in a populace. In the field of organic chemistry, the term polymorphism is utilized to depict unobtrusive basic contrasts in proteins that are generally indistinguishable.

Taking a gander at the Greek underlying foundations of the term polymorphism can assist with facilitating explaining the similarities between these definitions. The word poly signifies “many” and the word transform signifies “structure”, so when we talk about a polymorphism we’re looking at something that shows up in various structures.

This carries us to our software engineering meaning of polymorphism – we’ll offer three distinct forms for most extreme clearness.

  • Polymorphism is a feature in object-oriented programming languages that requires variables in various kinds to be used at different times within a particular routine.
  • Polymorphism is the ability of a programming language to have many different underlying data types with the same interface.
  • Polymorphism is the capacity of multiple objects to respond to the same message in a particular manner.

Definition of polymorphism

Polymorphism is an item’s volume to accept multiple kinds. When a recommendation to the parent class is worn to mention a child class object, the most frequent work of polymorphism in oop happens. It is assumed that every Java item that can proceed more than one is a trial assorted.

In biology, polymorphism is a discontinuous genetic variation that results in many distinct forms or types of individuals occurring among members of a single group. A discontinuous genetic variation separates a population’s individuals into two or more sharply different types.

It is necessary to realize that a recommendation changeable is the only possible means of accessing an entity. A recommendation changeable may only be of one kind. Until declared, it is difficult to modify the form of a reference variable. You may reassign the reference variable to other objects, providing that it is not deemed final. The methods that can be supplicated on the item can be defined by the form of the recommendation changeable. Every object of its declared type or any subtype of its announced kind may be referred to by a recommendation changeable. It is possible to declare a reference variable as a kind of class or device.

Example of Polymorphism

One of the main features of every object-oriented programming language is polymorphism. Polymorphism is all supported by languages like Ruby, Java, C++, and Python. Polymorphic programming enables a program, based on its data type or class, to process objects differently, with the ability to redefine methods for derived groups. Let ‘s analyze two of the most common examples used to explain polymorphism in computer science without going too far into descriptions of how to write polymorphic code (since it can be achieved differently based on the language you use).

Shapes

Polymorphism provides the opportunity for a software to redefine methods for related groups. Imagine that a piece of code is written where we describe a base class called form.

We will want to specify unique methods that can be extended to our base class, such as region and perimeter-but there’s an issue. Our understanding of geometry teaches us that the area and circumference of both a circle and a rectangle cannot be measured using the same tool. The area and perimeter of a circle depend, in turn, on its distance and, depending on its width and length, the area and perimeter of a rectangle.

We may describe a circle and a rectangle as sub-classes or derived classes of the base class shape in order to solve this problem using code polymorphism. Every subclass may have its own area and perimeter method, and various criteria may be agreed for each method-length and width for rectangles, radius for circles.

Our base case form now has two distinct classes-rectangle and circle-each with their own field and perimeter methods. The given collection of parameters can be used to decide which method to be named when we call one of the methods. If we have a single radius parameter, the methods that belong to the class circle can be named by our software. Our software will call the methods belonging to the class rectangle if we have two parameters for length and width. This represents a type of static polymorphism known as the overloading process.

Real life example of polymorphism

The phrase polymorphism suggests that it has multiple forms. We may define polymorphism, in simple words, the capacity of a communication to be shown in more than one shape. An example of polymorphism in real life: An individual may have distinct characteristics at the same time. Like a man, he is a father, a husband, and an employee at the same time.

Example in Java

We’re considering an instance of polymorphism here.

public interface Vegetarian {}

public class Bird {}

public class Dove extends Bird Implements Vegetarian {}

Still, as there are many inheritances, the Dove class is known to be polymorphic. For the examples listed above, the following is true:

A Dove is a Bird

A Dove is a Vegetarian

A Dove is a Dove

A Dove is an Object

The following assertions are valid when we attach the subject variable information to a Dove object reference.

Example

Dove d = new Dove ();

Bird b = d;

Vegetarian v = d;

Object o = d;

The reference variables d, b, v, o all refer to the same object as the Deer in the heap.

Types of Polymorphism

To solve problems in computer science and programming, there are several common methods of applying polymorphism. Five different ways of polymorphism that are widely used in different programming languages are briefly listed below.

  •   Ad-hoc Polymorphism
  •  Subtyping
  •  Parametric Polymorphism
  • Polytypism
  • Row Polymorphism

Ad-hoc Polymorphism

Ad-hoc polymorphism mentions the principle that, based on the kind of arguments on which it is applied, a similar function can be applied to different arguments and can work accordingly. The plus sign (+) operator is a fundamental example of a polymorphic function. In many ways, this operator can be used, including integer extension, floating-point extension, array concatenation, and string concatenation. When a single operator, based on the type of argument, executes multiple functions, we conclude that the operator is overloaded. Ad-hoc polymorphism is often appropriately referred to as function overloading of operator overloading.

Subtyping

We specified a class called curves in the above example and derived classes called circles and rectangles. In subtyping, we describe a super type that can have many subtypes that may also act on the various subtypes where separate functions or subroutines have been written to run on the super type.

The “Dogs” super type and subtypes such as “Golden Retrievers”, “Basset Hounds” and “Chihuahuas” could be described. Each subtype is a super type variety which shares some, but not all, of its features. Subtype polymorphism enables a feature to even act on objects that correspond to all of the subtypes of dogs that would usually run on a dog-type object. You can write a formula that takes a dog as a parameter, but since a basset hound is a subtype of dog, you can use basset hounds as a parameter as well.

Parametric Polymorphism

Parametric polymorphism enables programmers to write generic functions that, irrespective of their type, can handle values the same way. Put differently, a parametric function is a piece of code that, regardless of its form, handles all data in the same way. Parametric polymorphism is used, irrespective of the particular form of each element in the package, to write generic functions which operate on data sets.

Polytypism

In generic programming, Polytypism, also referred to as data form genericity, is used. Programmers design algorithms in generic programming where the forms can be defined at a later stage. The appropriate types can be given as parameters when the algorithm is instantiated at a later stage. A generic category is also defined as a type that is parameterized.

Row Polymorphism

A special form of polymorphism that deals with records in databases or spreadsheets, generally referred to as rows, is row polymorphism. It is possible to use row polymorphism to write programs that run only on a given portion of a list.

Sumo Logic Guards Against Polymorphic Threats

In order to build malicious code and scripts that alter the type, cyber attackers use polymorphism, including modifying their file name, encryption and compression tools. With anti-malware software, these systems are progressively difficult to spot. Researchers at Web root have recently found that 97% of malware infections use polymorphism to disable security steps.

Sumo Logic lets market enterprises detect ransomware and other unintended intrusions through behavior-based threat identification techniques that are more appropriate than conventional signature-based detection techniques for detecting polymorphic malware.

Implementation aspects of polymorphism

Static and dynamic polymorphism

Where the implementation is chosen, polymorphism can be defined by: constantly or dynamically. This is classified as static send and dynamic send, respectively, and static polymorphism and dynamic polymorphism are also considered the related modes of polymorphism. Static polymorphism is performed more easily when there is no dynamic overhead send, but extra accumulator brace is needed. In addition, static polymorphism allows accumulators, source program inspection instruments, and human editors to perform greater static analysis. For example, dynamic polymorphism facilitates duck typing, and a dynamically connected library will work on objects without understanding their complete form. Dynamic polymorphism is more stable, but slower.

In ad hoc polymorphism and parametric polymorphism, static polymorphism usually occurs, while for subtype polymorphism, dynamic polymorphism is normal. However, through the more advanced use of template metaprogramming, including the oddly repeating template sequence, it is possible to accomplish static polymorphism with subtyping. Static polymorphism becomes difficult for dynamic libraries when polymorphism is introduced through a module, since there is no way to know what types the parameters are until the shared object is constructed. Although languages like C++ and Rust use monomorphism templates, dynamic dispatch is used extensively by the Swift programming language to construct the binary interface of the application for these libraries by design. As a result, at the expense of runtime overhead, more code can be exchanged with a smaller device scale.

Advantage and Disadvantage of polymorphism

Advantage:

  1. It allows programmers to reuse, evaluate and execute the program, modules, forms written once. In certain aspects, they can be repeated.
  2.  You may use the odd variable name to stock variables of different types of data, such as Int, Float, etc.).
  3. Polymorphism tends to reduce the pairing of multiple functionalities.
  4. Method overloading can be extended to builders that allow multiple ways of initializing class objects. It helps you to identify several builders for managing various forms of initializations.
  5. Method overriding functions along with inheritance without the need for re-compilation to allow code reuse of existing groups.

Disadvantage:

  1. One of the key drawbacks of polymorphism is that the implementation of polymorphism in codes is complicated for developers.
  2. Polymorphism in run time will lead to the performance problem where the system has to determine which process or variable to invoke so that the performance is effectively diminished as decisions are taken at run time.
  3. The readability of the program is diminished by polymorphism. In order to define real execution time, one has to recognize the program’s runtime actions.

Why we need Polymorphism

Polymorphism only gives you advantages if you need polymorphism. It’s used anytime the specialization of another entity can be used as an individual in your intellectual idea. “Specialization” is the core term. The so-called taxonomy, which, for example, refers to living things, is a perfect example. People and dogs are also mammals. This suggests that all species that have such properties and behaviors in common are classified into the class of mammals.

A classification of a vehicle is both an ElectricCar and a DieselCar. So all you have is There Fuel so you expect to know if there’s enough fuel to run it when you drive a car. “Expectation” is another wonderful term.

Drawing an ER (entity relationship) diagram of the domain of your program before beginning it is always a fantastic idea. That’s because you’re required to guess what sort of entities are going to be generated and you can save a lot of code to identify similar behaviors between entities if you’re able enough. But code saving isn’t the only advantage of a successful project.

“You might be interested in finding out that so-called” software engineering “is a set of techniques and principles that enables you to write” clean code “(there is also a fantastic book called” Clean code “that programs sometimes suggest).

Benefits of polymorphism

  • Faster coding at runtime.
  • More effective runtime code.
  • At runtime, a more complex document.
  • More code that is modular and reusable.
  • Code that is covered by other groups from expansion.

How do we explain polymorphism?

Polymorphism is an object ‘s capacity to accept multiple types. Every Java object that is able to move more than one is called polymorphic by a test. This implies that in its parent hierarchy, every child class object may take every type of a class and, of course, itself.

Difference between Polymorphism and Inheritance

InheritancePolymorphism
Inheritance is one in which a new class (derived class) is formed that inherits the characteristics from the current class(base class).Whereas polymorphism is what can be characterized in different ways.
Basically, it is used for lessons.Whereas functions or techniques are simply implemented.
The principle of reusability is supported by inheritance and decreases code length in object-oriented programming.Polymorphism enables the object to determine which function type is to be enforced at both compile-time (overloading) and run-time (overriding).
Inheritance may be an inheritance of pure, mixed, mixed, hierarchical and multilevel.Whereas-time polymorphism (overload) as well as run-time polymorphism (overriding) can be compiled.
In pattern building, it is used.Since it is also used in creating patterns.

Overloading and Overriding in Polymorphism

Polymorphism refers to the tendency of multiple shapes to exist. In other words, the power of a reference variable to modify behavior depending on what instance of an entity it is.

Overloading:

  • Two or more methods within the same class that share a separate parameter list with the same name.
  •  Methods that are overloaded could vary in the form and/or number of their parameters.
  • Different return forms can be crowded.
  • It is also possible to overpower constructors.

Overriding:

  • Inside an inherited or subclass, override methods are redefined.
  • In sub groups of distinct process bodies, they have the same process signature.

Method Overloading

Concept Overloading has no relation with polymorphism. This applies to specifying various ways of a system (usually by providing different numbers or types of parameters). It can be used as a polymorphism that is unchanged. At coding time, the option to call an application or another is made. In this case, note that the signature of the system must alter.

Operator Overloading

Operator overloading is a separate polymorphism-related term that applies to a certain language-dependent operator ‘s tendency to function differently depending on the form of its operands (for example, + might mean concatenation with strings and addition with numeric operands).

Method Overriding

Method Overriding is when one of its subclasses re-defines a method specified in a superclass or gui, thereby modifying / replacing the action given by the superclass. Based on the object from which the operation is called, the decision to call an implementation or another is dynamically made at runtime. Note that when overriding, the signature of the process stays the same.

Summary

(1) Polymorphism is the occurrence of two or more distinct variants of a plant in the same ecosystem together in such quantities that the rarest of them cannot be preserved by repeated mutation.

(2) If there are even a few percent of a population with a genetically regulated type, it may have been preferred by option.

(3) Polymorphism may either be intermittent, in which a chromosome is in the process of rollout to an unopposed inhabitant, or regulated, in which the equilibrium of selective agencies is retained at a fixed stage.

(4) In general, due to the repeated creation of the variation, intermittent polymorphism is due to environmental changes that make the results of an earlier disadvantageous chromosome advantageous.

(5) Industrial melanism offers a remarkable example of intermittent polymorphism.

(6) The interpretation of this phenomena cannot be explained either on the basis of a specific collection of black forms in a developed world or on the basis of a prompt variation caused by mineral salts in food.

(7) On the opposite, manufacturing melanism is possibly due to the distribution of chromosomes that increase the survival of the plant, but those that manage for the development of excess flesh should not, in normal conditions, be used because it kills cryptic coloration. It is now understood that in the ordinary population, the genes that give rise to industrial melanism exist as rarities, and that they offer greater viability than the usual types they have been unable to replace. However, such genes can be found in manufacturing areas where a less extreme handicap is black coloring.

(8) Either environmental or genetic can be the variability involved in healthy polymorphism.

(9) In general, a balanced polymorphism requires a strong degree of permanence in the proportions of the respective forms.

(10) Moreover, differences in the relative frequencies of polymorphic forms can, in absolute numbers, obey cycles.

(11) Polymorphic forms may be distributed as clines that may or may not originate from monomorphic groups.

(12) While the presence of a transfer function that defines alternate forms involves regulated polymorphism, these are themselves capable of genetic modification within the polymorphism system.

(13) Near linkage based on sectional translocations of chromosomes is correlated with a special kind of regulated polymorphism.

(14) The polymorphism in heterostylous plants is due to the variations between the forms in terms in physiology rather than morphology.

(15) Sex itself, environmentally or genetically regulated, is a genuinely integrated polymorphism.

(16) Human polymorphisms have the ability (and symptoms related to them) to taste phenyl-thio-urea in low concentrations and in blood types. On both of them, the relative arguments of the transient and balanced structures of polymorphisms are considered.

 References:

  1. Chicha, Y., Lloyd, M., Oancea, C., & Watt, S. M. (2004). Parametric polymorphism for computer algebra software components. In Proc. 6th International Symposium on Symbolic and Numeric Algorithms for Scientific Comput (pp. 119-130).
  2. Ford, E. B. (1945). Polymorphism. Biological Reviews, 20(2), 73-88.
  3. https://onlinelibrary.wiley.com/doi/abs/10.1111/j.1469-185X.1945.tb00315.x
  4. https://www.sumologic.com/glossary/polymorphism/
  5. https://medium.com/@iroshan.du/method-overloading-overriding-in-java-83c252d40e96
  6. https://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading
  7. https://www.tutorialspoint.com/java/java_polymorphism.htm#:~:text=Polymorphism%20is%20the%20ability%20of,is%20considered%20to%20be%20polymorphic
  8. https://en.wikipedia.org/wiki/Polymorphism_(computer_science)