Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 16 / 68

Building a type hierarchy with abstract types

This lesson covers building a type hierarchy with Julia's abstract types, with the aim of organising types that share a common nature.

An abstract type defines a group of types that cannot itself be instantiated. Picture the abstract idea of an animal, with concrete types such as a dog and a cat hanging beneath it.

The sample code defines an abstract type with abstract type Animal end and declares Dog a subtype with struct Dog <: Animal. Note the <: symbol expressing the hierarchy.

A common early stumble is the difference between an abstract and a concrete type. An abstract type cannot be instantiated directly — it exists purely as a box for organising the hierarchy.

In professional work, this thinking is used wherever several types share an interface — kinds of animal, kinds of shape.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)