---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 15 Edition"
java_version: 15
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.10"
heading: "4.10. Subtyping"
parent: "Chapter 4. Types, Values, and Variables"
source: https://docs.oracle.com/javase/specs/jls/se15/html/jls-4.html#jls-4.10
id: "4.10"
url: https://jvm.obfuscated.software/jls/se15/4.10.html
raw: https://jvm.obfuscated.software/jls/se15/4.10.md
---

# 4.10. Subtyping

The subtype and supertype relations are binary relations on types.

The *supertypes* of a type are obtained by reflexive and transitive closure over the direct supertype relation, written S `>_1` T, which is defined by rules given later in this section. We write S `:>` T to indicate that the supertype relation holds between S and T.

S is a *proper supertype* of T, written S `>` T, if S `:>` T and S ≠ T.

The *subtypes* of a type T are all types U such that T is a supertype of U, and the null type. We write T `<:` S to indicate that that the subtype relation holds between types T and S.

T is a *proper subtype* of S, written T `<` S, if T `<:` S and S ≠ T.

T is a *direct subtype* of S, written T `<_1` S, if S `>_1` T.

Subtyping does not extend through parameterized types: T `<:` S does not imply that C`<`T`>` `<:` C`<`S`>`.

## 4.10.1. Subtyping among Primitive Types

The following rules define the direct supertype relation among the primitive types:

- `double` `>_1` `float`
- `float` `>_1` `long`
- `long` `>_1` `int`
- `int` `>_1` `char`
- `int` `>_1` `short`
- `short` `>_1` `byte`

## 4.10.2. Subtyping among Class and Interface Types

Given a non-generic type declaration C, the *direct supertypes* of the type C are all of the following:

- The direct superclass of C ([§8.1.4](8.1.4.md)).
- The direct superinterfaces of C ([§8.1.5](8.1.5.md)).
- The type `Object`, if C is an interface type with no direct superinterfaces ([§9.1.3](9.1.3.md)).

Given a generic type declaration C`<`F_1,...,F_n`>` (*n* > 0), the *direct supertypes* of the raw type C ([§4.8](4.8.md)) are all of the following:

- The direct superclass of the raw type C.
- The direct superinterfaces of the raw type C.
- The type `Object`, if C`<`F_1,...,F_n`>` is a generic interface type with no direct superinterfaces ([§9.1.2](9.1.2.md)).

Given a generic type declaration C`<`F_1,...,F_n`>` (*n* > 0), the *direct supertypes* of the generic type C`<`F_1,...,F_n`>` are all of the following:

- The direct superclass of C`<`F_1,...,F_n`>`.
- The direct superinterfaces of C`<`F_1,...,F_n`>`.
- The type `Object`, if C`<`F_1,...,F_n`>` is a generic interface type with no direct superinterfaces.
- The raw type C.

Given a generic type declaration C`<`F_1,...,F_n`>` (*n* > 0), the *direct supertypes* of the parameterized type C`<`T_1,...,T_n`>`, where T_i (1 ≤ *i* ≤ *n*) is a type, are all of the following:

- D`<`U_1 θ,...,U_k θ`>`, where D`<`U_1,...,U_k`>` is a generic type which is a direct supertype of the generic type C`<`F_1,...,F_n`>` and θ is the substitution `[F_1:=T_1,...,F_n:=T_n]`.
- C`<`S_1,...,S_n`>`, where S_i contains T_i (1 ≤ *i* ≤ *n*) ([§4.5.1](4.5.1.md)).
- The type `Object`, if C\<F_1,...,F_n> is a generic interface type with no direct superinterfaces.
- The raw type C.

Given a generic type declaration C`<`F_1,...,F_n`>` (*n* > 0), the *direct supertypes* of the parameterized type C`<`R_1,...,R_n`>` where at least one of the R_i (1 ≤ *i* ≤ *n*) is a wildcard type argument, are the direct supertypes of the parameterized type C`<`X_1,...,X_n`>` which is the result of applying capture conversion to C`<`R_1,...,R_n`>` ([§5.1.10](5.1.10.md)).

The direct supertypes of an intersection type T_1 `&` ... `&` T_n are T_i (1 ≤ *i* ≤ *n*).

The direct supertypes of a type variable are the types listed in its bound.

A type variable is a direct supertype of its lower bound.

The direct supertypes of the null type are all reference types other than the null type itself.

## 4.10.3. Subtyping among Array Types

The following rules define the direct supertype relation among array types:

- If S and T are both reference types, then S`[]` `>_1` T`[]` iff S `>_1` T.

- `Object` `>_1` `Object[]`

- `Cloneable` `>_1` `Object[]`

- `java.io.Serializable` `>_1` `Object[]`

- If P is a primitive type, then:

  - `Object` `>_1` P`[]`
  - `Cloneable` `>_1` P`[]`
  - `java.io.Serializable` `>_1` P`[]`

## 4.10.4. Least Upper Bound

The *least upper bound*, or "lub", of a set of reference types is a shared supertype that is more specific than any other shared supertype (that is, no other shared supertype is a subtype of the least upper bound). This type, lub(U_1, ..., U_k), is determined as follows.

If *k* = 1, then the lub is the type itself: lub(U) = U.

Otherwise:

- For each U_i (1 ≤ *i* ≤ *k*):

  Let ST(U_i) be the set of supertypes of U_i.

  Let EST(U_i), the set of erased supertypes of U_i, be:

  EST(U_i) = { |W| | W in ST(U_i) } where |W| is the erasure of W.

  The reason for computing the set of erased supertypes is to deal with situations where the set of types includes several distinct parameterizations of a generic type.

  For example, given `List<String>` and `List<Object>`, simply intersecting the sets ST(`List<String>`) = { `List<String>`, `Collection<String>`, `Object` } and ST(`List<Object>`) = { `List<Object>`, `Collection<Object>`, `Object` } would yield a set { `Object` }, and we would have lost track of the fact that the upper bound can safely be assumed to be a `List`.

  In contrast, intersecting EST(`List<String>`) = { `List`, `Collection`, `Object` } and EST(`List<Object>`) = { `List`, `Collection`, `Object` } yields { `List`, `Collection`, `Object` }, which will eventually enable us to produce `List<?>`.

- Let EC, the erased candidate set for U_1 ... U_k, be the intersection of all the sets EST(U_i) (1 ≤ *i* ≤ *k*).

- Let MEC, the minimal erased candidate set for U_1 ... U_k, be:

  MEC = { V | V in EC, and for all W ≠ V in EC, it is not the case that W `<:` V }

  Because we are seeking to infer more precise types, we wish to filter out any candidates that are supertypes of other candidates. This is what computing MEC accomplishes. In our running example, we had EC = { `List`, `Collection`, `Object` }, so MEC = { `List` }. The next step is to recover type arguments for the erased types in MEC.

- For any element G of MEC that is a generic type:

  Let the "relevant" parameterizations of G, Relevant(G), be:

  Relevant(G) = { V | 1 ≤ *i* ≤ *k*: V in ST(U_i) and V = G`<`...`>` }

  In our running example, the only generic element of MEC is `List`, and Relevant(`List`) = { `List<String>`, `List<Object>` }. We will now seek to find a type argument for `List` that contains ([§4.5.1](4.5.1.md)) both `String` and `Object`.

  This is done by means of the least containing parameterization (lcp) operation defined below. The first line defines lcp() on a set, such as Relevant(`List`), as an operation on a list consisting of the elements of the set. The next line defines the operation on such a list as a pairwise reduction on the elements of the list. The third line is the definition of lcp() on pairs of parameterized types, which in turn relies on the notion of least containing type argument (lcta). lcta() is defined for all possible cases.

  Let the "candidate" parameterization of G, Candidate(G), be the most specific parameterization of the generic type G that contains all the relevant parameterizations of G:

  Candidate(G) = lcp(Relevant(G))

  where lcp(), the least containing parameterization, is:

  - lcp(S) = lcp(`e_1`, ..., `e_n`) where `e_i` (1 ≤ *i* ≤ *n*) in S
  - lcp(`e_1`, ..., `e_n`) = lcp(lcp(`e_1`, `e_2`), `e_3`, ..., `e_n`)
  - lcp(G`<`X_1, ..., X_n`>`, G`<`Y_1, ..., Y_n`>`) = G`<`lcta(X_1, Y_1), ..., lcta(X_n, Y_n)`>`
  - lcp(G`<`X_1, ..., X_n`>`) = G`<`lcta(X_1), ..., lcta(X_n)`>`

  and where lcta(), the least containing type argument, is: (assuming U and V are types)

  - lcta(U, V) = U if U = V, otherwise `?` `extends` lub(U, V)
  - lcta(U, `?` `extends` V) = `?` `extends` lub(U, V)
  - lcta(U, `?` `super` V) = `?` `super` glb(U, V)
  - lcta(`?` `extends` U, `?` `extends` V) = `?` `extends` lub(U, V)
  - lcta(`?` `extends` U, `?` `super` V) = `?`
  - lcta(`?` `super` U, `?` `super` V) = `?` `super` glb(U, V)
  - lcta(U) = `?` if U's upper bound is `Object`, otherwise `?` `extends` lub(U,`Object`)

  and where glb() is as defined in [§5.1.10](5.1.10.md).

- Let lub(U_1 ... U_k) be:

  Best(W_1) `&` ... `&` Best(W_r)

  where W_i (1 ≤ *i* ≤ *r*) are the elements of MEC, the minimal erased candidate set of U_1 ... U_k;

  and where, if any of these elements are generic, we use the candidate parameterization (so as to recover type arguments):

  Best(X) = Candidate(X) if X is generic; X otherwise.

Strictly speaking, this lub() function only approximates a least upper bound. Formally, there may exist some other type T such that all of U_1 ... U_k are subtypes of T and T is a subtype of lub(U_1, ..., U_k). However, a compiler for the Java programming language must implement lub() as specified above.

It is possible that the lub() function yields an infinite type. This is permissible, and a compiler for the Java programming language must recognize such situations and represent them appropriately using cyclic data structures.

The possibility of an infinite type stems from the recursive calls to lub(). Readers familiar with recursive types should note that an infinite type is not the same as a recursive type.

## 4.10.5. Type Projections

A *synthetic type variable* is a type variable introduced by the compiler during capture conversion ([§5.1.10](5.1.10.md)) or inference variable resolution ([§18.4](18.4.md)).

It is sometimes necessary to find a close supertype of a type, where that supertype does not mention certain synthetic type variables. This is achieved with an *upward projection* applied to the type.

Similarly, a *downward projection* may be applied to find a close subtype of a type, where that subtype does not mention certain synthetic type variables. Because such a type does not always exist, downward projection is a partial function.

These operations take as input a set of type variables that should no longer be referenced, referred to as the *restricted type variables*. When the operations recur, the set of restricted type variables is implicitly passed on to the recursive application.

The upward projection of a type T with respect to a set of restricted type variables is defined as follows:

- If T does not mention any restricted type variable, then the result is T.

- If T is a restricted type variable, then the result is the upward projection of the upper bound of T.

- If T is a parameterized class type or a parameterized interface type, G`<`A_1,...,A_n`>`, then the result is G`<`A_1',...,A_n'`>`, where, for 1 ≤ *i* ≤ *n*, A_i' is derived from A_i as follows:

  - If A_i does not mention any restricted type variable, then A_i' = A_i.

  - If A_i is a type that mentions a restricted type variable, then let U be the upward projection of A_i. A_i' is a wildcard, defined by three cases:

    - If U is not `Object`, and if either the declared bound of the *i*th parameter of G, B_i, mentions a type parameter of G, or B_i is not a subtype of U, then A_i' is an upper-bounded wildcard, `?` `extends` U.
    - Otherwise, if the downward projection of A_i is `L`, then A_i' is a lower-bounded wildcard, `?` `super` `L`.
    - Otherwise, the downward projection of A_i is undefined and A_i' is an unbounded wildcard, `?`.

  - If A_i is an upper-bounded wildcard that mentions a restricted type variable, then let U be the upward projection of the wildcard bound. A_i' is an upper-bounded wildcard, `?` `extends` U.

  - If A_i is a lower-bounded wildcard that mentions a restricted type variable, then if the downward projection of the wildcard bound is `L`, then A_i' is a lower-bounded wildcard, `?` `super` `L`; if the downward projection of the wildcard bound is undefined, then A_i' is an unbounded wildcard, `?`.

- If T is an array type, S`[]`, then the result is an array type whose component type is the upward projection of S.

- If T is an intersection type, then the result is an intersection type. For each element, S, of T, the result has as an element the upward projection of S.

The downward projection of a type T with respect to a set of restricted type variables is a partial function, defined as follows:

- If T does not mention any restricted type variable, then the result is T.

- If T is a restricted type variable, then if T has a lower bound, and if the downward projection of that bound is `L`, the result is `L`; if T has no lower bound, or if the downward projection of that bound is undefined, then the result is undefined.

- If T is a parameterized class type or a parameterized interface type, G`<`A_1,...,A_n`>`, then the result is G`<`A_1',...,A_n'`>`, if, for 1 ≤ *i* ≤ *n*, a type argument A_i' can be derived from A_i as follows; if not, the result is undefined:

  - If A_i is does not mention a restricted type variable, then A_i' = A_i.
  - If A_i is a type that mentions a restricted type variable, then A_i' is undefined.
  - If A_i is an upper-bounded wildcard that mentions a restricted type variable, then if the downward projection of the wildcard bound is U, then A_i' is an upper-bounded wildcard, `?` `extends` U; if the downward projection of the wildcard bound is undefined, then A_i' is undefined.
  - If A_i is a lower-bounded wildcard that mentions a restricted type variable, then let `L` be the upward projection of the wildcard bound. A_i' is a lower-bounded wildcard, `?` `super` `L`.

- If T is an array type, S`[]`, then if the downward projection of S is S', the result is S'`[]`; if the downward projection of S is undefined, then the result is undefined.

- If T is an intersection type, then if the downward projection is defined for *each* element of T, the result is an intersection type whose elements are the downward projections of the elements of T; if the downward projection is undefined for *any* element of T, then the result is undefined.

Like lub ([§4.10.4](4.10.4.md)), upward projection and downward projection may produce infinite types, due to the recursion on type variable bounds.
