Home

typesubtype

Typesubtype, commonly referred to simply as subtyping, is a relationship between types in a type system. If type A is a subtype of type B, values of A can be used wherever a value of B is expected without causing type errors. This containment means every value of A is also a value of B, but B may have additional values that A does not possess.

In programming languages with inheritance, subtyping often comes from class hierarchies: if A extends B, then

Formal rules of subtyping can vary. A common notation is A ≤ B to denote that A is

When parameterized types are involved, subtyping becomes more nuanced. Variance explains how subtyping propagates through type

Subtyping is central to type safety and language design. It supports flexible interfaces and code reuse while

A
is
a
subtype
of
B.
For
example,
a
Dog
is
a
subtype
of
Animal,
so
a
function
that
expects
an
Animal
can
be
given
a
Dog.
Subtyping
supports
polymorphism,
enabling
code
that
works
with
a
general
type
to
also
work
with
its
specific
subtypes.
a
subtype
of
B.
In
addition
to
nominal
typing
based
on
declared
inheritance,
some
systems
use
structural
typing
where
subtyping
depends
on
the
actual
shape
or
members
of
a
type
rather
than
its
name.
constructors:
covariant,
contravariant,
or
invariant
positions
determine
whether
a
container
of
A
can
be
used
where
a
container
of
B
is
expected.
For
example,
function
types
and
generic
collections
rely
on
these
rules
to
preserve
type
safety.
requiring
careful
handling
of
casts
and
conversions.
Related
concepts
include
the
Liskov
substitution
principle,
type
hierarchies,
and
both
nominal
and
structural
type
systems.