Home » Polymorphism (programming)

Polymorphism (programming)

1967
  • Christopher Strachey
Computer programming workspace demonstrating polymorphism with code snippets.

Polymorphism, from Greek for “many shapes,” allows objects of different classes to be treated as objects of a common superclass. It enables a single interface, such as a method name, to be used for a general class of actions. The specific action is determined by the exact type of the object at runtime. This is often achieved through method overriding.

Polymorphism is a powerful concept that allows for writing flexible and decoupled code. The most common form in OOP is subtype polymorphism, which is enabled by inheritance and method overriding. When a subclass provides a specific implementation for a method that is already defined in its superclass, it is called overriding. A variable of the superclass type can refer to an object of any of its subclasses. When a method is called on this variable, the version of the method that gets executed is the one belonging to the object’s actual class, not the variable’s type. This decision is made at runtime, a process known as dynamic dispatch or late binding.

For example, consider a superclass ‘Shape’ with a method ‘calculateArea()’. Subclasses like ‘Circle’ and ‘Rectangle’ can inherit from ‘Shape’ and provide their own specific implementations of ‘calculateArea()’. A program can then have a list of ‘Shape’ objects, containing both ‘Circle’ and ‘Rectangle’ instances. When iterating through the list and calling ‘calculateArea()’ on each element, the correct formula will be used for each shape automatically. This eliminates the need for long ‘if-else’ or ‘switch’ statements to check the type of each object, leading to cleaner, more extensible code. New shapes can be added to the system without modifying the code that processes them.

UNESCO Nomenclature: 1203
– Computer science

Type

Abstract System

Disruption

Incremental

Usage

Widespread Use

Precursors

  • the concept of function pointers in languages like c
  • generic programming concepts
  • inheritance, which provides the class hierarchy necessary for subtype polymorphism
  • the need for flexible and extensible software systems

Applications

  • drawing different shapes (‘circle’, ‘square’) using a single ‘draw()’ method call
  • processing various document types (‘pdf’, ‘word’) with a generic ‘print()’ function
  • implementing user interface event handlers
  • generic collections that can hold objects of different but related types
  • dependency injection frameworks

Patents:

NA

Potential Innovations Ideas

Professionals (100% free) Membership Required

You must be a Professionals (100% free) member to access this content.

Join Now

Already a member? Log in here
Related to: polymorphism, overriding, dynamic dispatch, late binding, subtype, interface, abstraction, oop, method overriding, runtime.

Leave a Reply

Your email address will not be published. Required fields are marked *

AVAILABLE FOR NEW CHALLENGES
Mechanical Engineer, Project, Process Engineering or R&D Manager
Effective product development

Available for a new challenge on short notice.
Contact me on LinkedIn
Plastic metal electronics integration, Design-to-cost, GMP, Ergonomics, Medium to high-volume devices & consumables, Lean Manufacturing, Regulated industries, CE & FDA, CAD, Solidworks, Lean Sigma Black Belt, medical ISO 13485

We are looking for a new sponsor

 

Your company or institution is into technique, science or research ?
> send us a message <

Receive all new articles
Free, no spam, email not distributed nor resold

or you can get your full membership -for free- to access all restricted content >here<

Historical Context

(if date is unknown or not relevant, e.g. "fluid mechanics", a rounded estimation of its notable emergence is provided)

Related Invention, Innovation & Technical Principles

Scroll to Top

You May Also Like