Polymorphism (computer science): Difference between revisions

Content deleted Content added
Subtyping: Mentioned a relation between subtypes and a supertype in an example of them.
Replace "class" and "type" with "form" when talking about approaches to polymorphism. (Do not use "kind", because, as both "class" and "type", it's a term of type theory itself.)
Line 6:
{{cite web | url=http://www.stroustrup.com/glossary.html#Gpolymorphism | author=Bjarne Stroustrup | title=Bjarne Stroustrup's C++ Glossary | date=February 19, 2007 | quote=polymorphism – providing a single interface to entities of different types.}}</ref> or the use of a single symbol to represent multiple different types.<ref name="Luca">{{Cite journal | last1 = Cardelli | first1 = Luca| author-link1 = Luca Cardelli| last2 = Wegner | first2 = Peter| author-link2 = Peter Wegner| doi = 10.1145/6041.6042| title = On understanding types, data abstraction, and polymorphism| journal = [[ACM Computing Surveys]]| volume = 17| issue = 4| pages = 471–523| date=December 1985 | url = http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf| citeseerx = 10.1.1.117.695| s2cid = 2921816}}: "Polymorphic types are types whose operations are applicable to values of more than one type."</ref> The concept is borrowed from a principle in biology where an organism or species can have many different forms or stages.<ref name="Moved">{{cite web | title=Polymorphism |work=The Java™ Tutorials: Learning the Java Language: Interfaces and Inheritance |publisher=Oracle | url=https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html | access-date=2021-09-08}}</ref>
 
The most commonly recognized major classesforms of polymorphism are:
* ''[[Ad hoc polymorphism]]'': defines a common interface for an arbitrary set of individually specified types.
* ''[[Parametric polymorphism]]'': not specifying concrete types and instead use abstract symbols that can substitute for any type.
Line 16:
In a 1985 paper, [[Peter Wegner]] and [[Luca Cardelli]] introduced the term ''inclusion polymorphism'' to model subtypes and [[Inheritance (object-oriented programming)|inheritance]],<ref name="Luca"/> citing [[Simula]] as the first programming language to implement it.
 
==TypesForms==
===Ad hoc polymorphism===
{{main|Ad hoc polymorphism}}
[[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]]).<ref name=Strachey00/> The term "[[ad hoc]]" in this context is not intended to be pejorative; it refers simply to the fact that this typeform of polymorphism is not a fundamental feature of the type system. In the [[Pascal (programming language)|Pascal]] / [[Delphi (programming language)|Delphi]] example below, the <code>Add</code> functions seem to work generically over two types (integer and string) when looking at the invocations, but are considered to be two entirely distinct functions by the compiler for all intents and purposes:
 
<syntaxhighlight lang="Pascal">