Polymorphism (computer science): Difference between revisions

Content deleted Content added
JuUunIOr (talk | contribs)
No edit summary
 
(6 intermediate revisions by 5 users not shown)
Line 2:
{{Distinguish|Polymorphic code}}
{{Polymorphism}}
In [[programming language theory]] and [[type theory]], '''polymorphism''' is the useapproach ofthat oneallows symbola tovalue representtype to multipleassume different types.<ref name="Luca">{{Cite journal |last1=Cardelli |first1=Luca |author1-link=Luca Cardelli |last2=Wegner |first2=Peter |author2-link=Peter Wegner (computer scientist)|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>
 
In [[object-oriented programming]], polymorphism is the provision of one [[Interface (object-oriented programming)|interface]] to entities of different [[data type]]s.<ref>{{cite web |url=http://www.stroustrup.com/glossary.html#Gpolymorphism |last1=Stroustrup |first1=Bjarne |author1-link=Bjarne Stroustrup |title=Bjarne Stroustrup's C++ Glossary |date=February 19, 2007 |quote=polymorphism – providing a single interface to entities of different types.}}</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>
Line 14:
Interest in polymorphic [[type system]]s developed significantly in the 1990s, with practical implementations beginning to appear by the end of the decade. ''Ad hoc polymorphism'' and ''parametric polymorphism'' were originally described in [[Christopher Strachey]]'s ''[[Fundamental Concepts in Programming Languages]]'',<ref name=Strachey00>{{cite journal |last1=Strachey |first1=Christopher |author1-link=Christopher Strachey |date=2000 |title=Fundamental Concepts in Programming Languages |journal=[[Higher-Order and Symbolic Computation]] |volume=13 |issue=1/2 |pages=11–49 |doi=10.1023/A:1010000313106 |issn=1573-0557 |citeseerx=10.1.1.332.3161 |s2cid=14124601}}</ref> where they are listed as "the two main classes" of polymorphism. Ad hoc polymorphism was a feature of [[ALGOL 68]], while parametric polymorphism was the core feature of [[ML (programming language)|ML]]'s [[type system]].
 
In a 1985 paper, [[Peter Wegner (computer scientist)|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.
 
==Forms==
===Ad hoc polymorphism===
{{Further|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 pejorative: instead, it means that this form of polymorphism is not a fundamental feature of the type system. In the [[Java (programming language)|Java]] example below, the <code>Addadd</code> functions seem to work generically over two types ([[Integer (computer science)|integer]] and [[String (computer science)|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="Javajava">
class AdHocPolymorphic {
public String add(int x, int y) {
return "Sum: " + (x + y);
}
 
public String add(String name) {
return "Added " + name;
}
}
 
public class adhocAdhoc {
public static void main(String[] args) {
AdHocPolymorphic poly = new AdHocPolymorphic();
 
System.out.println( poly.add(1,2)); ); // prints "Sum: 3"
System.out.println( poly.add("Jay") ); // prints "Added Jay"
}
}
Line 109:
 
static void letsHear(final Pet pet) {
System.out.println(pet.speak());
}
 
Line 158:
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|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]].