liveBook · Manning

Class hierarchy

As explained in Julia as a Second Language

A type hierarchy is an arrangement of types in parent-child relationships that distinguishes abstract types from concrete types.

A game may have archers, pikemen, and knights that are all warriors. In Julia, a type hierarchy expresses that relationship through an abstract parent type and concrete child types. Unlike class hierarchies in many object-oriented languages, Julia’s hierarchies also include numbers, strings, and collections. section 7.3.1 section 7.2

Follow a number’s ancestry

Start with `typeof(42)`, which returns `Int64`. Repeatedly calling `supertype` follows `Int64 → Signed → Integer → Real → Number → Any`. `Any` is the top: `supertype(Any)` returns `Any`. Travel downward with `subtypes(Number)` to find `Complex` and `Real`, then inspect their children. Use `Int8 <: Integer` to ask whether one type is a subtype of another; it returns `true`. section 7.2

The distinction between abstract and concrete matters when creating values. `Warrior` can group related types, but `Warrior()` cannot create an object; a concrete `Archer` can. Only leaf nodes of a Julia type hierarchy can be concrete, and a concrete type cannot be subtyped. section 7.3.1 section 7.5

Listing 7.5 Definition of types in battle simulator
abstract type Warrior end           #1
 
mutable struct Archer <: Warrior    #2
    name::String
    health::Int
    arrows::Int
end
 
mutable struct Pikeman <: Warrior
    name::String
    health::Int
end
 
mutable struct Knight <: Warrior
    name::String
    health::Int
    mounted::Bool                   #3
 end

This battle-simulator definition places three concrete `mutable struct` types beneath `abstract type Warrior`. Each declares its own fields: an abstract type cannot contain fields, so shared `name` and `health` fields are a convention here, not an enforced inheritance rule. The `<:` syntax declares the parent relationship. section 7.3.1

The hierarchy has practical consequences. An `Any`-typed array accepts a string alongside integers, while assigning a string to an `Integer`-typed array fails. Types also help select methods: Julia’s multiple dispatch considers the types of all arguments, rather than only the first. section 7.2 section 7.5

How Julia as a Second Language covers it

How deep it goes
Works through ancestry queries and subtype declarations, while noting that shared fields in the example are conventional rather than enforced.
Examples
Traces `Int64` to `Any`, tests subtype relationships, and groups archers, pikemen, and knights beneath a warrior type.
What it assumes
Builds from ordinary values and `typeof`, introducing hierarchy exploration through REPL calls rather than assuming prior type-system knowledge.
What you can do after
Build it from scratch with `abstract type` and `mutable struct`, then inspect ancestry and test subtype relationships in Julia.
How it connects
Connects abstract types, concrete types, composite types, typed arrays, and multiple dispatch.

Also explained in

Compare how 4 books explain class hierarchy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.