---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 9 Edition"
java_version: 9
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/se9/html/jls-4.html#jls-4.10
id: "4.10"
url: https://jvm.obfuscated.software/jls/se9/4.10.html
raw: https://jvm.obfuscated.software/jls/se9/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.
