Polymorphism (computer science): Difference between revisions

Content deleted Content added
Subtyping: add UML image
Make paragraph match code
Line 87:
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 animalspets. The procedure <code>letsHear()</code> accepts ana animalpet, but will also work correctly if a subtype is passed to it:
 
<syntaxhighlight lang="java">