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
Subtyping
(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!
==Relationship with inheritance== Subtyping and inheritance are independent (orthogonal) relationships. They may coincide, but none is a special case of the other. In other words, between two types ''S'' and ''T'', all combinations of subtyping and inheritance are possible: # ''S'' is neither a subtype nor a derived type of ''T'' # ''S'' is a subtype but is not a derived type of ''T'' # ''S'' is not a subtype but is a derived type of ''T'' # ''S'' is both a subtype and a derived type of ''T'' The first case is illustrated by independent types, such as <code>Boolean</code> and <code>Float</code>. The second case can be illustrated by the relationship between <code>Int32</code> and <code>Int64</code>. In most object oriented programming languages, <code>Int64</code> are unrelated by inheritance to <code>Int32</code>. However <code>Int32</code> can be considered a subtype of <code>Int64</code> since any 32 bit integer value can be promoted into a 64 bit integer value. {{Original research|date=July 2022}} The third case is a consequence of [[Subtyping of functions|function subtyping input contravariance]]. Assume a super class of type ''T'' having a method ''m'' returning an object of the same type (''i.e.'' the type of ''m'' is ''T'' β ''T'', also note that the first parameter of ''m'' is this/self) and a derived class type ''S'' from ''T''. By inheritance, the type of ''m'' in ''S'' is ''S'' β ''S''.{{Citation needed|date=July 2022}} In order for ''S'' to be a subtype of ''T'' the type of ''m'' in ''S'' must be a subtype of the type of ''m'' in ''T'' {{Citation needed|date=July 2022}}, in other words: ''S'' β ''S'' β€: ''T'' β ''T''. By bottom-up application of the function subtyping rule, this means: ''S'' β€: ''T'' and ''T'' β€: ''S'', which is only possible if ''S'' and ''T'' are the same. Since inheritance is an irreflexive relation, ''S'' can't be a subtype of ''T''. Subtyping and inheritance are compatible when all inherited fields and methods of the derived type have types which are subtypes of the corresponding fields and methods from the inherited type .{{sfn|Cook|Hill|Canning|1990}}
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)