Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Polymorphism (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===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>add</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="java"> class AdHocPolymorphic { public String add(int x, int y) { return "Sum: " + (x + y); } public String add(String name) { return "Added " + name; } } public class Adhoc { 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" } } </syntaxhighlight> 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".<ref name="Luca"/><ref name="Tucker2004">{{cite book |last1=Tucker |first1=Allen B.|date=2004 |title=Computer Science Handbook |edition=2nd |url=https://books.google.com/books?id=9IFMCsQJyscC&pg=SA91-PA5 |publisher=Taylor & Francis |pages=91β |isbn=978-1-58488-360-9}}</ref>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)