Julia Abstract Types Tutorial
last modified November 18, 2026
An abstract type is a name for a family of related types. It cannot be instantiated itself, but concrete types can declare that they belong to it. Abstract types let us write code against a concept — such as "a number" or "a shape" — instead of a single concrete representation.
This tutorial explains how to declare a type hierarchy, explore it with
supertype and subtypes, and use the special types
Any, Union, Nothing, and
Missing. The examples were verified with Julia 1.13.4.
Declaring an Abstract Type
An abstract type is introduced with abstract type ... end. A
concrete struct declares its place in the hierarchy with the
subtype operator <:.
abstract type Animal end struct Dog <: Animal end struct Cat <: Animal end println(Dog <: Animal) println(Dog <: Any) println(typeof(Dog()))
We define an abstract Animal and two concrete subtypes,
Dog and Cat. The expression Dog <:
Animal asks whether Dog is a subtype of Animal,
and it is true. Constructing Dog() gives an object whose type is
the concrete Dog, not the abstract parent — abstract types have no
instances of their own.
$ julia main.jl true true Dog
supertype and subtypes
Julia provides introspection functions for walking the type tree:
supertype moves up one level and subtypes lists the
direct children.
using InteractiveUtils println(supertype(Int)) println(supertype(Integer)) println(supertype(Float64)) println(subtypes(Integer))
The InteractiveUtils standard library supplies
subtypes; in a script it must be imported explicitly (the REPL
loads it automatically). The parent of Int is Signed,
the parent of Integer is Real, and the parent of
Float64 is AbstractFloat. The result of
subtypes(Integer) is an array of type objects, printed as
Any[Bool, Signed, Unsigned].
$ julia main.jl Signed Real AbstractFloat Any[Bool, Signed, Unsigned]
The Numeric Type Tree
Julia's number types are organized as a tree, and the subtype relation follows the branches upward.
using InteractiveUtils println(Real <: Number) println(Int <: Integer <: Real <: Number <: Any) println(subtypes(Real))
The <: operator can be chained: Int is an
Integer, which is a Real, which is a
Number, which is an Any, so the whole chain evaluates
to true. The children of Real are the abstract families
AbstractFloat, AbstractIrrational,
Integer, and Rational.
$ julia main.jl true true Any[AbstractFloat, AbstractIrrational, Integer, Rational]
Any — the Top Type
Any sits at the root of the type tree: every type is a subtype of
it, and every value is an instance of it.
println(Any) println(Int <: Any) println(String <: Any) println(nothing isa Nothing) println(typeof(nothing))
Both Int and String are subtypes of
Any. The value nothing, used to signal the absence of
a value, has its own concrete type Nothing; it is of course also an
instance of Any. Because Any is so broad, annotating a
variable or argument with it gives the compiler little information.
$ julia main.jl Any true true true Nothing
Union Types
A Union is an abstract type that contains exactly the listed
types. It is useful when a value may be one of a small set of types.
alias = Union{Int, Float64}
println(alias)
println(3 isa alias)
println(3.5 isa alias)
println("x" isa alias)
println(Union{Nothing, String})
An integer and a float both belong to the union, while a string does not. Note
that Julia normalizes the printed order of the members, so
Union{Int, Float64} is displayed as
Union{Float64, Int64}. A very common union is
Union{Nothing, T}, meaning "either a value of type
T or no value at all."
$ julia main.jl
Union{Float64, Int64}
true
true
false
Union{Nothing, String}
Nothing and Missing
Julia has two distinct ways to represent absence: nothing means
"no value", while missing means "a value exists but is unknown".
println(missing isa Missing) println(typeof(missing)) println(nothing === Nothing()) println(ismissing(missing)) println(isnothing(nothing))
The single instance of Nothing is nothing, so calling
the type as a constructor returns the same object and the identity check
=== is true. The predicates isnothing and
ismissing are the readable way to test for these values.
$ julia main.jl true Missing true true true
Unions in Function Signatures
Union types are often used to restrict an argument to a couple of acceptable types while still allowing "no value".
function status(x::Union{Nothing, String})
if x === nothing
return "no value"
else
return "value: $x"
end
end
println(status(nothing))
println(status("ready"))
The status function accepts either nothing or a
string. Inside the function, x === nothing distinguishes the two
cases; because the type is restricted, the code in the other branch can safely
treat x as a string. This pattern is idiomatic for optional
arguments.
$ julia main.jl no value value: ready
Building a Custom Hierarchy
Abstract types can be nested to build a hierarchy that mirrors the problem domain.
using InteractiveUtils abstract type Shape end abstract type Polygon <: Shape end struct Circle <: Shape end struct Triangle <: Polygon end println(subtypes(Shape)) println(subtypes(Polygon)) println(supertypes(Triangle))
Polygon is an abstract child of Shape;
Circle attaches directly to Shape, while
Triangle attaches to Polygon. The
subtypes function lists only direct children, and
supertypes returns the full chain from a type up to
Any as a tuple.
$ julia main.jl Any[Circle, Polygon] Any[Triangle] (Triangle, Polygon, Shape, Any)
Subtype Checks in Practice
Subtype checks work with unions and base types just as they do with custom hierarchies.
println(Int <: Union{Int, Float64})
println(Union{Int, Float64} <: Real)
println(promote_type(Int, Float64))
An Int is one member of the union, so the first check is true. The
whole union is a subtype of Real because both of its members are.
The common type to which Int and Float64 promote is
Float64. Understanding these relations is the key to reading
Julia's method selection, which is the subject of multiple dispatch.
$ julia main.jl true true Float64
Best Practices for Abstract Types
A few guidelines keep hierarchies useful rather than decorative.
- Prefer abstract supertypes in signatures: an argument
annotated
AbstractVectoraccepts many concrete array types. - Do not expect to instantiate an abstract type: declare a
concrete
structfor objects that hold data. - Use
Union{Nothing, T}for optional values: it documents the two possibilities in the type itself. - Import
InteractiveUtilsin scripts: it providessubtypes,supertypes, and@whichwhen the REPL is not in use.
Source
Julia Abstract Types documentation
In this article we have explored Julia's type tree: declaring abstract and
concrete types, navigating them with subtype checks, supertype,
and subtypes, and using Any,
Union, Nothing, and Missing. The concrete
types themselves are introduced in Julia data
types, and composite types are built in Julia
structs. This hierarchy is what makes Julia multiple dispatch possible.
Author
List all Julia tutorials.