Home

Subtyping

Subtyping is a relation between types in a programming language where one type, called a subtype, is considered a more specific version of another type, its supertype. Values of the subtype can be used wherever a value of the supertype is expected, without altering the program’s correctness. Subtyping enables polymorphism and code reuse by allowing operations to be written in terms of the supertype and applied to any of its subtypes.

The relationship is often described in terms of substitutability. The Liskov Substitution Principle specifies that objects

Subtyping systems can be nominal or structural. In nominal typing, subtyping is declared explicitly through inheritance

Subtyping interacts closely with generics and variance. Variance describes how subtyping relates to type parameters: covariant,

Common considerations include coercions and upcasts, where values are treated as their supertype, and the potential

of
a
subtype
should
be
usable
wherever
objects
of
the
supertype
are
expected,
preserving
expected
behavior.
This
principle
supports
safe
extension
and
refinement
of
types
in
object-oriented
and
related
paradigms.
or
interface
implementation.
In
structural
typing,
subtyping
is
determined
by
the
shape
of
types,
such
as
the
presence
of
compatible
members,
regardless
of
explicit
declarations.
Languages
vary
in
whether
they
adopt
nominal
or
structural
subtyping;
for
example,
Java
uses
nominal
typing,
while
many
typing
systems
for
functional
or
scripting
languages
use
structural
rules
or
a
mix
of
both.
contravariant,
or
invariant,
affecting
how
subtypes
propagate
through
parameterized
types.
Subtyping
also
interfaces
with
type
safety
and
language
design,
influencing
how
easily
code
can
be
reused
and
how
robust
the
type
system
remains.
for
runtime
type
errors
if
subtyping
rules
are
violated.