SlideShare a Scribd company logo
1 of 38
OOP
 In programming languages and type
theory, polymorphism is the provision of a
single interface to entities of different types or
the use of a single symbol to represent multiple
different types.
 The most commonly recognized major classes
of polymorphism are:
 Ad hoc polymorphism: defines a common
interface for an arbitrary set of individually
specified types.
 Parametric polymorphism: when one or more
types are not specified by name but by abstract
symbols that can represent any type.
 Subtyping (also called subtype
polymorphism or inclusion polymorphism): when a
name denotes instances of many different
classes related by some common superclass.
 Christopher Strachey chose the term ad hoc
polymorphism to refer to polymorphic functions
that can be applied to arguments of different
types, but that behave differently depending
on the type of the argument to which they are
applied (also known as function
overloading or operator overloading).[5]
 The term "ad hoc" in this context is not
intended to be pejorative; it refers simply to the
fact that this type of polymorphism is not a
fundamental feature of the type system.
 In the Pascal / Delphi example below,
the Add functions seem to work generically
over various types when looking at the
invocations, but are considered to be two
entirely distinct functions by the compiler for
all intents and purposes:
 In dynamically typed languages the situation
can be more complex as the correct function
that needs to be invoked might only be
determinable at run time.
 Implicit type conversion has also been defined
as a form of polymorphism, referred to as
"coercion polymorphism".
 Parametric polymorphism allows a function or a
data type to be written generically, so that it
can handle values uniformly without depending
on their type.[7] Parametric polymorphism is a
way to make a language more expressive while
still maintaining full static type-safety.
 The concept of parametric polymorphism
applies to both data types and functions. A
function that can evaluate to or be applied to
values of different types is known as
a polymorphic function.
 A data type that can appear to be of a
generalized type (e.g. a list with elements of
arbitrary type) is designated polymorphic data
type like the generalized type from which such
specializations are made.

 Parametric polymorphism is ubiquitous in
functional programming, where it is often
simply referred to as "polymorphism".
 The following example in Haskell shows a
parameterized list data type and two
parametrically polymorphic functions on them:
 Parametric polymorphism is also available in
several object-oriented languages. For
instance, templates in C++ and D, or under the
name generics in C#, Delphi and Java:
 John C. Reynolds (and later Jean-Yves Girard)
formally developed this notion of
polymorphism as an extension to lambda
calculus (called the polymorphic lambda
calculus or System F).
 Any parametrically polymorphic function is
necessarily restricted in what it can do,
working on the shape of the data instead of its
value, leading to the concept of parametricity.
 Some languages employ the idea
of subtyping (also called subtype
polymorphism or inclusion polymorphism) to
restrict the range of types that can be used in a
particular case of polymorphism.
 In these languages, subtyping allows a
function to be written to take an object of a
certain type T, but also work correctly, if
passed an object that belongs to a type S that is
a subtype of T (according to the Liskov
substitution principle). This type relation is
sometimes written S <: T.
 Conversely, T is said to be a supertype of S—
written T :> S. Subtype polymorphism is
usually resolved dynamically (see below).
 In the following example we make cats and
dogs subtypes of animals. The
procedure letsHear() accepts an animal, but
will also work correctly if a subtype is passed
to it:
 In another example, if Number, Rational,
and Integer are types such
that Number :> Rational and Number :> Integer, a
function written to take a Number will work
equally well when passed
an Integer or Rational as when passed a Number.
 The actual type of the object can be hidden
from clients into a black box, and accessed via
object identity. In fact, if the Number type
is abstract, it may not even be possible to get
your hands on an object whose most-
derived type is Number (see abstract data
type, abstract class).
 This particular kind of type hierarchy is
known—especially in the context of the Scheme
programming language—as a numerical tower,
and usually contains many more types.
 Object-oriented programming languages offer
subtype polymorphism using subclassing (also
known as inheritance).
 In typical implementations, each class contains
what is called a virtual table—a table of
functions that implement the polymorphic part
of the class interface—and each object contains
a pointer to the "vtable" of its class, which is
then consulted whenever a polymorphic
method is called. This mechanism is an
example of:
 late binding, because virtual function calls are
not bound until the time of invocation;
 single dispatch (i.e. single-argument
polymorphism), because virtual function calls
are bound simply by looking through the
vtable provided by the first argument
(the this object), so the runtime types of the
other arguments are completely irrelevant.
 The same goes for most other popular object
systems. Some, however, such as Common
Lisp Object System, provide multiple dispatch,
under which method calls are polymorphic
in all arguments.
 The interaction between parametric
polymorphism and subtyping leads to the
concepts of variance and bounded
quantification.
 Row polymorphism[8] is a similar, but distinct
concept from subtyping. It deals
with structural types. It allows the usage of all
values whose types have certain properties,
without losing the remaining type information.
 A related concept is polytypism (or data type
genericity). A polytypic function is more
general than polymorphic, and in such a
function, "though one can provide fixed ad hoc
cases for specific data types, an ad hoc
combinator is absent".
 Polymorphism can be distinguished by when
the implementation is selected: statically (at
compile time) or dynamically (at run time,
typically via a virtual function). This is known
respectively as static dispatch and dynamic
dispatch, and the corresponding forms of
polymorphism are accordingly called static
polymorphism and dynamic polymorphism.
 Static polymorphism executes faster, because
there is no dynamic dispatch overhead, but
requires additional compiler support. Further,
static polymorphism allows greater static
analysis by compilers (notably for
optimization), source code analysis tools, and
human readers (programmers).
 Dynamic polymorphism is more flexible but
slower—for example, dynamic polymorphism
allows duck typing, and a dynamically linked
library may operate on objects without
knowing their full type.
 Static polymorphism typically occurs in ad hoc
polymorphism and parametric polymorphism,
whereas dynamic polymorphism is usual for
subtype polymorphism.
 However, it is possible to achieve static
polymorphism with subtyping through more
sophisticated use of template
metaprogramming, namely the curiously
recurring template pattern.
 When polymorphism is exposed via a library,
static polymorphism becomes impossible
for dynamic libraries as there is no way of
knowing what types the parameters are when
the shared object is built.
 See also
 While languages like C++ and Rust use
monomorphized templates, the Swift
programming language makes extensive use of
dynamic dispatch to build the application
binary interface for these libraries by default.
As a result, more code can be shared for a
reduced system size at the cost of runtime
overhead.

More Related Content

What's hot

Doppl development iteration #4
Doppl development   iteration #4Doppl development   iteration #4
Doppl development iteration #4Diego Perini
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020VigneshVijay21
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answerVasuki Ramasamy
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answersMadhavendra Dutt
 
Xtext: type checking and scoping
Xtext: type checking and scopingXtext: type checking and scoping
Xtext: type checking and scopingMikhail Barash
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Ralf Laemmel
 
Tdm probabilistic models (part 2)
Tdm probabilistic  models (part  2)Tdm probabilistic  models (part  2)
Tdm probabilistic models (part 2)KU Leuven
 
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...WebStackAcademy
 
Information for learning object exchange
Information for learning object exchangeInformation for learning object exchange
Information for learning object exchangeDavid Massart
 
Text classification-php-v4
Text classification-php-v4Text classification-php-v4
Text classification-php-v4Glenn De Backer
 

What's hot (20)

Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
Doppl development iteration #4
Doppl development   iteration #4Doppl development   iteration #4
Doppl development iteration #4
 
Python Interview questions 2020
Python Interview questions 2020Python Interview questions 2020
Python Interview questions 2020
 
Unit 1 question and answer
Unit 1 question and answerUnit 1 question and answer
Unit 1 question and answer
 
Java interview questions and answers
Java interview questions and answersJava interview questions and answers
Java interview questions and answers
 
Xtext: type checking and scoping
Xtext: type checking and scopingXtext: type checking and scoping
Xtext: type checking and scoping
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)Metaprograms and metadata (as part of the the PTT lecture)
Metaprograms and metadata (as part of the the PTT lecture)
 
Interpreter
InterpreterInterpreter
Interpreter
 
Turbo prolog 2.0 basics
Turbo prolog 2.0 basicsTurbo prolog 2.0 basics
Turbo prolog 2.0 basics
 
Tdm probabilistic models (part 2)
Tdm probabilistic  models (part  2)Tdm probabilistic  models (part  2)
Tdm probabilistic models (part 2)
 
Reg Ex
Reg ExReg Ex
Reg Ex
 
Ontology matching
Ontology matchingOntology matching
Ontology matching
 
Core java questions
Core java questionsCore java questions
Core java questions
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...Core Java Programming Language (JSE) : Chapter III -  Identifiers, Keywords, ...
Core Java Programming Language (JSE) : Chapter III - Identifiers, Keywords, ...
 
Information for learning object exchange
Information for learning object exchangeInformation for learning object exchange
Information for learning object exchange
 
Reclassification
ReclassificationReclassification
Reclassification
 
Text classification-php-v4
Text classification-php-v4Text classification-php-v4
Text classification-php-v4
 
CORBA IDL
CORBA IDLCORBA IDL
CORBA IDL
 

Similar to Polymorphism

Similar to Polymorphism (20)

Polymorphism in Python
Polymorphism in PythonPolymorphism in Python
Polymorphism in Python
 
Object-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedObject-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism Unleashed
 
LectureNotes-01-DSA
LectureNotes-01-DSALectureNotes-01-DSA
LectureNotes-01-DSA
 
What is java polymorphism and its types in java training?
What is  java polymorphism and its types in java training?What is  java polymorphism and its types in java training?
What is java polymorphism and its types in java training?
 
Polymorphism and its types
Polymorphism and its typesPolymorphism and its types
Polymorphism and its types
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Substitutability
SubstitutabilitySubstitutability
Substitutability
 
Mit gnu scheme reference manual
Mit gnu scheme reference manualMit gnu scheme reference manual
Mit gnu scheme reference manual
 
Oop
OopOop
Oop
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Mcs024
Mcs024Mcs024
Mcs024
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for Scala
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdf
 
Oop by edgar lagman jr
Oop by edgar lagman jr Oop by edgar lagman jr
Oop by edgar lagman jr
 
C++ first s lide
C++ first s lideC++ first s lide
C++ first s lide
 
Ti1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: PolymorphismTi1220 Lecture 7: Polymorphism
Ti1220 Lecture 7: Polymorphism
 
Type Classes
Type ClassesType Classes
Type Classes
 
Perl Reference.ppt
Perl Reference.pptPerl Reference.ppt
Perl Reference.ppt
 
Python 3.x quick syntax guide
Python 3.x quick syntax guidePython 3.x quick syntax guide
Python 3.x quick syntax guide
 
LISP: Scope and extent in lisp
LISP: Scope and extent in lispLISP: Scope and extent in lisp
LISP: Scope and extent in lisp
 

Recently uploaded

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Polymorphism

  • 1. OOP
  • 2.  In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types.
  • 3.  The most commonly recognized major classes of polymorphism are:  Ad hoc polymorphism: defines a common interface for an arbitrary set of individually specified types.  Parametric polymorphism: when one or more types are not specified by name but by abstract symbols that can represent any type.
  • 4.  Subtyping (also called subtype polymorphism or inclusion polymorphism): when a name denotes instances of many different classes related by some common superclass.
  • 5.  Christopher Strachey chose the term ad hoc polymorphism to refer to polymorphic functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading).[5]
  • 6.  The term "ad hoc" in this context is not intended to be pejorative; it refers simply to the fact that this type of polymorphism is not a fundamental feature of the type system.
  • 7.  In the Pascal / Delphi example below, the Add functions seem to work generically over various types when looking at the invocations, but are considered to be two entirely distinct functions by the compiler for all intents and purposes:
  • 8.
  • 9.  In dynamically typed languages the situation can be more complex as the correct function that needs to be invoked might only be determinable at run time.  Implicit type conversion has also been defined as a form of polymorphism, referred to as "coercion polymorphism".
  • 10.  Parametric polymorphism allows a function or a data type to be written generically, so that it can handle values uniformly without depending on their type.[7] Parametric polymorphism is a way to make a language more expressive while still maintaining full static type-safety.
  • 11.  The concept of parametric polymorphism applies to both data types and functions. A function that can evaluate to or be applied to values of different types is known as a polymorphic function.
  • 12.  A data type that can appear to be of a generalized type (e.g. a list with elements of arbitrary type) is designated polymorphic data type like the generalized type from which such specializations are made. 
  • 13.  Parametric polymorphism is ubiquitous in functional programming, where it is often simply referred to as "polymorphism".  The following example in Haskell shows a parameterized list data type and two parametrically polymorphic functions on them:
  • 14.
  • 15.
  • 16.  Parametric polymorphism is also available in several object-oriented languages. For instance, templates in C++ and D, or under the name generics in C#, Delphi and Java:
  • 17.  John C. Reynolds (and later Jean-Yves Girard) formally developed this notion of polymorphism as an extension to lambda calculus (called the polymorphic lambda calculus or System F).
  • 18.  Any parametrically polymorphic function is necessarily restricted in what it can do, working on the shape of the data instead of its value, leading to the concept of parametricity.
  • 19.  Some languages employ the idea of subtyping (also called subtype polymorphism or inclusion polymorphism) to restrict the range of types that can be used in a particular case of polymorphism.
  • 20.  In these languages, subtyping allows a function to be written to take an object of a certain type T, but also work correctly, if passed an object that belongs to a type S that is a subtype of T (according to the Liskov substitution principle). This type relation is sometimes written S <: T.
  • 21.  Conversely, T is said to be a supertype of S— written T :> S. Subtype polymorphism is usually resolved dynamically (see below).
  • 22.  In the following example we make cats and dogs subtypes of animals. The procedure letsHear() accepts an animal, but will also work correctly if a subtype is passed to it:
  • 23.
  • 24.  In another example, if Number, Rational, and Integer are types such that Number :> Rational and Number :> Integer, a function written to take a Number will work equally well when passed an Integer or Rational as when passed a Number.
  • 25.  The actual type of the object can be hidden from clients into a black box, and accessed via object identity. In fact, if the Number type is abstract, it may not even be possible to get your hands on an object whose most- derived type is Number (see abstract data type, abstract class).
  • 26.  This particular kind of type hierarchy is known—especially in the context of the Scheme programming language—as a numerical tower, and usually contains many more types.  Object-oriented programming languages offer subtype polymorphism using subclassing (also known as inheritance).
  • 27.  In typical implementations, each class contains what is called a virtual table—a table of functions that implement the polymorphic part of the class interface—and each object contains a pointer to the "vtable" of its class, which is then consulted whenever a polymorphic method is called. This mechanism is an example of:
  • 28.  late binding, because virtual function calls are not bound until the time of invocation;  single dispatch (i.e. single-argument polymorphism), because virtual function calls are bound simply by looking through the vtable provided by the first argument (the this object), so the runtime types of the other arguments are completely irrelevant.
  • 29.  The same goes for most other popular object systems. Some, however, such as Common Lisp Object System, provide multiple dispatch, under which method calls are polymorphic in all arguments.  The interaction between parametric polymorphism and subtyping leads to the concepts of variance and bounded quantification.
  • 30.  Row polymorphism[8] is a similar, but distinct concept from subtyping. It deals with structural types. It allows the usage of all values whose types have certain properties, without losing the remaining type information.
  • 31.  A related concept is polytypism (or data type genericity). A polytypic function is more general than polymorphic, and in such a function, "though one can provide fixed ad hoc cases for specific data types, an ad hoc combinator is absent".
  • 32.  Polymorphism can be distinguished by when the implementation is selected: statically (at compile time) or dynamically (at run time, typically via a virtual function). This is known respectively as static dispatch and dynamic dispatch, and the corresponding forms of polymorphism are accordingly called static polymorphism and dynamic polymorphism.
  • 33.  Static polymorphism executes faster, because there is no dynamic dispatch overhead, but requires additional compiler support. Further, static polymorphism allows greater static analysis by compilers (notably for optimization), source code analysis tools, and human readers (programmers).
  • 34.  Dynamic polymorphism is more flexible but slower—for example, dynamic polymorphism allows duck typing, and a dynamically linked library may operate on objects without knowing their full type.
  • 35.  Static polymorphism typically occurs in ad hoc polymorphism and parametric polymorphism, whereas dynamic polymorphism is usual for subtype polymorphism.
  • 36.  However, it is possible to achieve static polymorphism with subtyping through more sophisticated use of template metaprogramming, namely the curiously recurring template pattern.
  • 37.  When polymorphism is exposed via a library, static polymorphism becomes impossible for dynamic libraries as there is no way of knowing what types the parameters are when the shared object is built.  See also
  • 38.  While languages like C++ and Rust use monomorphized templates, the Swift programming language makes extensive use of dynamic dispatch to build the application binary interface for these libraries by default. As a result, more code can be shared for a reduced system size at the cost of runtime overhead.