Content deleted Content added
Tassedethe (talk | contribs) No edit summary |
→Ad hoc polymorphism: format code |
||
Line 21:
[[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="
class AdHocPolymorphic {
public String add(int x, int y) {
return "Sum: " + (x + y);
}
public String add(String name) {
return "Added " + name;
}
}
public class
public static void main(String[] args) {
AdHocPolymorphic poly = new AdHocPolymorphic();
System.out.println(
System.out.println(
}
}
|