Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Polymorphism (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Subtyping=== {{Further|Subtyping}} 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 {{nowrap|''S'' <: ''T''}}. Conversely, ''T'' is said to be a ''supertype'' of ''S'', written {{nowrap|''T'' :> ''S''}}. Subtype polymorphism is usually resolved dynamically (see below). In the following Java example cats and dogs are made subtypes of pets. The procedure <code>letsHear()</code> accepts a pet, but will also work correctly if a subtype is passed to it: <syntaxhighlight lang="java"> abstract class Pet { abstract String speak(); } class Cat extends Pet { String speak() { return "Meow!"; } } class Dog extends Pet { String speak() { return "Woof!"; } } static void letsHear(final Pet pet) { println(pet.speak()); } static void main(String[] args) { letsHear(new Cat()); letsHear(new Dog()); } </syntaxhighlight> [[File:UML class pet.svg]] In another example, if ''Number'', ''Rational'', and ''Integer'' are types such that {{nowrap|''Number'' :> ''Rational''}} and {{nowrap|''Number'' :> ''Integer''}} (''Rational'' and ''Integer'' as subtypes of a type ''Number'' that is a supertype of them), a function written to take a ''Number'' will work equally well when passed an ''Integer'' or ''Rational'' as when passed a ''Number''. The actual type of the object can be hidden from clients into a [[black box]], and accessed via object [[identity (object-oriented programming)|identity]]. If the ''Number'' type is ''abstract'', it may not even be possible to get your hands on an object whose ''most-derived'' type is ''Number'' (see [[abstract data type]], [[abstract class]]). This particular kind of type hierarchy is known, especially in the context of the [[Scheme (programming language)|Scheme language]], as a ''[[numerical tower]]'', and usually contains many more types. [[Object-oriented programming language]]s offer subtype polymorphism using ''[[Subclass (computer science)|subclass]]ing'' (also known as ''[[inheritance in object-oriented programming|inheritance]]''). In typical implementations, each class contains what is called a ''[[virtual table]]'' (shortly called ''vtable'') — a table of functions that implement the polymorphic part of the class interface—and each object contains a pointer to the vtable of its class, which is then consulted whenever a polymorphic method is called. This mechanism is an example of: * ''[[late binding]]'', because virtual function calls are not bound until the time of invocation; * ''[[single dispatch]]'' (i.e., single-argument polymorphism), because virtual function calls are bound simply by looking through the vtable provided by the first argument (the <code>this</code> object), so the runtime types of the other arguments are completely irrelevant. The same goes for most other popular object systems. Some, however, such as [[Common Lisp Object System]], provide ''[[multiple dispatch]]'', under which method calls are polymorphic in ''all'' arguments. The interaction between parametric polymorphism and subtyping leads to the concepts of [[covariance and contravariance (computer science)|variance]] and [[bounded quantification]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)