Content deleted Content added
No edit summary Tags: Mobile edit Mobile app edit iOS app edit App section source |
No edit summary Tags: Mobile edit Mobile web edit |
||
(One intermediate revision by one other user not shown) | |||
Line 2:
{{Distinguish|Polymorphic code}}
{{Polymorphism}}
In [[programming language theory]] and [[type theory]], '''polymorphism''' is the
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 24:
class AdHocPolymorphic {
public String add(int x, int y) {
return String.format("Sum: %d",
}
public String add(String name) {
return String.format("Added "
}
}
Line 36:
AdHocPolymorphic poly = new AdHocPolymorphic();
System.out.println(poly.add(1, 2)); // prints "Sum: 3"
System.out.println(poly.add("Jay")); // prints "Added Jay"
}
|