Content deleted Content added
Add Go to the list (supports it since 1.18) |
m included the language of the example |
||
Line 86:
Some languages employ the idea of ''subtyping'' (also called ''subtype polymorphism'' or ''inclusion polymorphism'') to restrict the range of types that can be used in a particular case of polymorphism. In these languages, subtyping allows a function to be written to take an object of a certain type ''T'', but also work correctly, if passed an object that belongs to a type ''S'' that is a subtype of ''T'' (according to the [[Liskov substitution principle]]). This type relation is sometimes written ''S'' <: ''T''. Conversely, ''T'' is said to be a ''supertype'' of ''S''—written ''T'' :> ''S''. Subtype polymorphism is usually resolved dynamically (see below).
In the following Java example we make cats and dogs subtypes of animals. The procedure <code>letsHear()</code> accepts an animal, but will also work correctly if a subtype is passed to it:
<syntaxhighlight lang="Java">
|