encyclopegia.org

Subtyping

In programming language theory, subtyping (also called subtype polymorphism or inclusion polymorphism) is a form of type polymorphism. A subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability, meaning that program elements (typically subroutines or functions), written to operate on elements of the supertype, can also operate on elements of the subtype. If S is a subtype of T, the subtyping relation (written as , , or ) means that any term of type S can safely be used in any context where a term of type T is expected. The precise semantics of subtyping here crucially depends on the particulars of how "safely be used" and "any context" are defined by a given type formalism or programming language. The type system of a programming language essentially defines its own subtyping relation, which may well be trivial, should the language support no (or very little) conversion mechanisms. Due to the subtyping relation, a term may belong to more than one type. Subtyping is therefore a form of type polymorphism. In object-oriented programming the term 'polymorphism' is commonly used to refer solely to this subtype polymorphism, while the techniques of parametric polymorphism would be considered generic programming. Functional programming languages often allow the subtyping of records.…

In programming language theory, subtyping (also called subtype polymorphism or inclusion polymorphism) is a form of type polymorphism. A subtype is a datatype that is related to another datatype (the supertype) by some notion of substitutability, meaning that program elements (typically subroutines or functions), written to operate on elements of the supertype, can also operate on elements of the subtype. If S is a subtype of T, the subtyping relation (written as ,   , or    ) means that any term of type S can safely be used in any context where a term of type T is expected. The precise semantics of subtyping here crucially depends on the particulars of how "safely be used" and "any context" are defined by a given type formalism or programming language. The type system of a programming language essentially defines its own subtyping relation, which may well be trivial, should the language support no (or very little) conversion mechanisms. Due to the subtyping relation, a term may belong to more than one type. Subtyping is therefore a form of type polymorphism. In object-oriented programming the term 'polymorphism' is commonly used to refer solely to this subtype polymorphism, while the techniques of parametric polymorphism would be considered generic programming. Functional programming languages often allow the subtyping of records. Consequently, simply typed lambda calculus extended with record types is perhaps the simplest theoretical setting in which a useful notion of subtyping may be defined and studied. Because the resulting calculus allows terms to have more than one type, it is no longer a "simple" type theory. Since functional programming languages, by definition, support function literals, which can also be stored in records, records types with subtyping provide some of the features of object-oriented programming. Typically, functional programming languages also provide some, usually restricted, form of parametric polymorphism. In a theoretical setting, it is desirable to study the interaction of the two features; a common theoretical setting is system F <: . Various calculi that attempt to capture the theoretical properties of object-oriented programming may be derived from system F <: . The concept of subtyping is related to the linguistic notions of hyponymy and holonymy. It is also related to the concept of bounded quantification in mathematical logic (see Order-sorted logic). Subtyping should not be confused with the notion of (class or object) inheritance from object-oriented languages; subtyping is a relation between types (interfaces in object-oriented parlance) whereas inheritance is a relation between implementations stemming from a language feature that allows new objects to be created from existing ones. In a number of object-oriented languages, subtyping is called interface inheritance, with inheritance referred to as implementation inheritance. function max (x as Number, y as Number) is if x If integer and real are both subtypes of Number , and an operator of comparison with an arbitrary Number is defined for both types, then values of either type can be passed to this function. However, the very possibility of implementing such an operator highly constrains the Number type (for example, one can't compare an integer with a complex number), and actually only comparing integers with integers, and reals with reals, makes sense. Rewriting this function so that it would only accept 'x' and 'y' of the same type requires bounded polymorphism. Subtyping enables a given type to be substituted for another type or abstraction. Subtyping is said to establish an is-a relationship between the subtype and some existing abstraction, either implicitly or explicitly, depending on language support. The relationship can be expressed explicitly via inheritance in languages that support inheritance as a subtyping mechanism. ===C++=== The following C++ code establishes an explicit inheritance relationship between classes B and A, where B is both a subclass and a subtype of A, and can be used as an A wherever a B is specified (via a reference, a pointer or the object itself). class A { public: void methodOfA() const { // ... } }; class B : public A { public: void methodOfB() const { // ... } }; void functionOnA(const A& a) { a.methodOfA(); } int main() { B b; functionOnA(b); // b can be substituted for an A. } ===Python=== The following python code establishes an explicit inheritance relationship between classes and , where is both a subclass and a subtype of , and can be used as an wherever a is required. class A: def method_of_a(self) -> None: pass class B(A): def method_of_b(self) -> None: pass def function_on_a(a: A) -> None: a.method_of_a() if __name__ == "__main__": b: B = B() function_on_a(b) # b can be substituted for an A. The following example, is a "regular" type, and is a metatype. While as distributed all types have the same metatype ( , which is also its own metatype), this is not a requirement. The type of classic classes, known as , can also be considered a distinct metatype. a = 0 print(type(a)) prints: print(type(type(a))) prints: print(type(type(type(a)))) prints: print(type(type(type(type(a))))) prints: ===Java=== In Java, is-a relation between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses. Using the classes, implements , and extends . So is a subtype of , which is a subtype of . The subtyping relationship is preserved between the types automatically. When defining an interface, , that associates an optional value of generic type P with each element, its declaration might look like: interface PayloadList extends List { void setPayload(int index, P val); ... } The following parameterizations of PayloadList are subtypes of : PayloadList PayloadList PayloadList T = \{v \in D \mid \ P_T(v)\} S =\{v \in D \mid \ P_T(v)\text{ and }P_s(v)\} The predicate \mathbf T = P_T is applied alongside P_s as part of the compound predicate S defining . The two predicates are conjoined, so both must be true for a value to be selected. The predicate \mathbf S = \mathbf T \land P_s = P_T \land P_s subsumes the predicate T, so For example: there is a subfamily of cat species called Felinae, which is part of the family Felidae. The genus Felis, to which the domestic cat species Felis catus belongs, is part of that subfamily. \mathit{Felinae = \{cat \in Felidae \mid \ ofSubfamily(cat, felinaeSubfamilyName)\}} \mathit{Felis =\{cat \in Felinae \mid \ ofGenus(cat, felisGenusName)\}} The conjunction of predicates has been expressed here through application of the second predicate over the domain of values conforming to the first predicate. Viewed as types, . If T subsumes S (T :> S) then a procedure, function or expression given a value s \in S as an operand (parameter value or term) will therefore be able to operate over that value as one of type T, because s \in T . In the example above, we could expect the function ofSubfamily to be applicable to values of all three types Felidae, Felinae and Felis. ==Function types== If is a function type, then a subtype of it is any function type with the property that and This can be summarised using the following typing rule: {T_1 \leq: S_1 \quad S_2 \leq: T_2} \over {S_1 \rightarrow S_2 \leq: T_1 \rightarrow T_2} The parameter type of is said to be contravariant because the subtyping relation is reversed for it, whereas the return type is covariant. Informally, this reversal occurs because the refined type is "more liberal" in the types it accepts and "more conservative" in the type it returns. This is what exactly works in Scala: a n-ary function is internally a class that inherits the \mathtt{ Function_N({-A_1}, {-A_2}, \dots, {-A_n}, {+B})} trait (which can be seen as a general interface in Java-like languages), where \mathtt{A_1, A_2, \dots, A_n} are the parameter types, and \mathtt{B} is its return type; "−" before the type means the type is contravariant while "+" means covariant. In languages that allow side effects, like most object-oriented languages, subtyping is generally not sufficient to guarantee that a function can be safely used in the context of another. Liskov's work in this area focused on behavioral subtyping, which besides the type system safety discussed in this article also requires that subtypes preserve all invariants guaranteed by the supertypes in some contract. This definition of subtyping is generally undecidable, so it cannot be verified by a type checker. The subtyping of mutable references is similar to the treatment of parameter values and return values. Write-only references (or sinks) are contravariant, like parameter values; read-only references (or sources) are covariant, like return values. Mutable references which act as both sources and sinks are invariant. ==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 Boolean and Float . The second case can be illustrated by the relationship between Int32 and Int64 . In most object oriented programming languages, Int64 are unrelated by inheritance to Int32 . However Int32 can be considered a subtype of Int64 since any 32 bit integer value can be promoted into a 64 bit integer value. The third case is a consequence of 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. 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 , 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 . ==Coercions== In coercive subtyping systems, subtypes are defined by explicit type conversion functions from subtype to supertype. For each subtyping relationship (S S → T (s) of type T. A coercion function may be defined by composition: if S T → U ∘ coerce S → T ). The type coercion from a type to itself coerce T → T is the identity function id T . Coercion functions for records and disjoint union subtypes may be defined componentwise; in the case of width-extended records, type coercion simply discards any components which are not defined in the supertype. The type coercion for function types may be given by f(t) = coerce S 2 → T 2 (f(coerce T 1 → S 1 (t))), reflecting the contravariance of parameter values and covariance of return values. The coercion function is uniquely determined given the subtype and supertype. Thus, when multiple subtyping relationships are defined, one must be careful to guarantee that all type coercions are coherent. For instance, if an integer such as 2 : int can be coerced to a floating point number (say, 2.0 : float), then it is not admissible to coerce 2.1 : float to 2 : int, because the compound coercion coerce float → float given by coerce int → float ∘ coerce float → int would then be distinct from the identity coercion id float . ==Notes==

References

No reference rows published for this article.

Cite this article

MLA

"Subtyping." encyclopegia.org, 2026, https://encyclopegia.org/wiki/subtyping. Accessed 24 November 2026.

Chicago

"Subtyping." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/subtyping.

APA

encyclopegia.org. (2026). Subtyping. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/subtyping

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1375955403 · 0.5 · snapshot enwiki-001375955403

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.