Subtype 213508 222067890 2008-06-27T12:10:17Z Thijs!bot 1392310 robot Adding: [[pl:Podtyp (programowanie)]] In [[computer science]], a '''subtype''' is a [[datatype]] that is generally related to another datatype (the '''supertype''') by some notion of ''[[substitutability]]'', meaning that [[computer program]]s written to operate on elements of the supertype can also operate on elements of the subtype. More specifically, the supertype-subtype relation is often taken to be the one defined by the [[Liskov substitution principle]]; however, any given [[programming language]] may define its own notion of subtyping, or none at all. == Example == [[Image:Inheritance.gif|thumb|right|350px|Example of Subtypes: Birds]] A simple practical example of subtypes is shown in the diagram, right. The generic object "bird" (or, supertype) gives rise to three derived objects (or, subtypes) "duck", "cuckoo" and "ostrich". Each of these varies the basic "bird" concept in its own way, but still inherits many "bird" characteristics. A data object may be declared as any one of the four types. The [[Unified Modeling Language|UML]] notation is used in this diagram, with open-headed arrows showing the direction and type of the relationship between the supertype and its subtypes. In most class-based [[Object-oriented programming language|object-oriented]] languages, [[Subclass (computer science)|subclass]]es give rise to subtypes: if ''A'' is a subclass of ''B'', then an instance of class ''A'' may be used in any context where an instance of type ''B'' is expected; thus we say ''A'' is a subtype of ''B''. A consequence of this is that any variable or parameter declared as having type ''B'' might, at run time, hold a value of class ''A''; in this situation many object-oriented programmers would say that ''B'' is the variable's ''static type'' and ''A'' is its ''dynamic type''. Exceptions to this rule include [[private inheritance]] in [[C++]] (which does not create subtypes), and certain operations on derived types in [[Eiffel (programming language)|Eiffel]], wherein features inherited from a base class can be removed or modified in a way which violates the subtyping rule. As another example, a language might allow integer values to be used wherever floating-point values are expected, or it might define a type <tt>number</tt> containing both the integers and the reals. In the first instance, the integer type would be a subtype of the floating-point type; in the second, those two types might be unrelated to each other by subtyping but both would be subtypes of <tt>number</tt>. Programmers may take advantage of subtyping to write code in a more abstract manner than would be possible without it. Consider the following example: function max (x as number, y as number) is if x < y then return y else return x end If integer and real are both subtypes of <code>number</code>, then values of either type can be passed to this function. Because of this, subtyping is often considered a form of [[Polymorphism (computer science)|polymorphism]]. The above example can also be compared to the [[Template (programming)|template feature]] of C++. In [[type theory]], the subtype relation is often written <tt>&lt;:</tt>, so that ''A''<tt>&lt;:</tt>''B'' means that ''A'' is a subtype of ''B''. Subtyping in type theory is characterized by the fact that any expression of type ''A'' may also be given type ''B'' if ''A''<tt>&lt;:</tt>''B''; the formal [[inference rule|typing rule]] that codifies this is known as the ''subsumption'' rule. == Subtyping schemes == Type theorists make a distinction between '''[[nominative type system|nominal subtyping]]''', in which only types declared in a certain way may be subtypes of each other, and '''[[structural type system|structural subtyping]]''', in which the structure of two types determines whether or not one is a subtype of the other. The class-based object-oriented subtyping described above is nominal; a structural subtyping rule for an object-oriented language might say that if objects of type ''A'' can handle all of the messages that objects of type ''B'' can handle (that is, if they define all the same [[Method (computer science)|method]]s), then ''A'' is a subtype of ''B'' regardless of whether either [[inheritance (computer science)|inherits]] from the other. Sound structural subtyping rules for types other than object types are also well known. Implementations of programming languages with subtyping fall into two general classes: ''inclusive'' implementations, in which the representation of any value of type ''A'' also represents the same value at type ''B'' if ''A''<tt>&lt;:</tt>''B'', and ''coercive'' implementations, in which a value of type ''A'' can be ''automatically converted'' into one of type ''B''. The subtyping induced by subclassing in an object-oriented language is usually inclusive; subtyping relations that relate integers and floating-point numbers, which are represented differently, are usually coercive. In almost all type systems that define a subtyping relation, it is reflexive (meaning ''A''<tt>&lt;:</tt>''A'' for any type ''A'') and transitive (meaning that if ''A''<tt>&lt;:</tt>''B'' and ''B''<tt>&lt;:</tt>''C'' then ''A''<tt>&lt;:</tt>''C''). This makes it a [[preorder]] on types. == See also == {{wikibooks|Ada Programming|Type System|Subtypes}} * A [[derived type]] is a [[type]] given a new type but structurally the same as the original type. It may or may not be a subtype depending on the type system. * Contravariance in [[Covariance and contravariance (computer science)]] * The [[circle-ellipse problem]] for the perils of subtyping variable-types on the same basis as value-types * [[class-based programming]] for an example of a problem when one confuses subtyping with [[Subclass (computer science)|subclassing]]. *[[Top type]] *[[Superclass (computer science)|superclass]] and [[Subclass (computer science)|subclass]] == External links == * ''Object-Oriented Software Construction'' by [[Bertrand Meyer]] [[Category:Data types]] [[Category:Type theory]] [[bn:উপটাইপ (কম্পিউটার বিজ্ঞান)]] [[it:Sottotipo (informatica)]] [[ja:派生型]] [[pl:Podtyp (programowanie)]] [[zh:子类型]]