Content deleted Content added
m minor fix |
→Ad hoc polymorphism: Use a more familiar/commonly used language, as measured in 2023 StackOverflow dev survey's "Most Popular Technologies" |
||
Line 20:
===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 form of polymorphism is not a fundamental feature of the type system. In the [[
<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 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>
|