Crystal Classes — Inheritance, Abstract Classes, and Method Overrides
DodaTech
Updated 2026-06-29
1 min read
In this tutorial, you will learn about Crystal Classes. We cover key concepts, practical examples, and best practices to help you master this topic.
Crystal has a single-inheritance class system with modules for mixins. Classes define behaviour and carry state through instance variables.
Class Basics
class Animal
getter name : String
def initialize(@name : String)
end
def speak : String
"#{@name} makes a sound"
end
end
class Dog < Animal
def speak
"#{@name} barks!"
end
end
dog = Dog.new("Rex")
puts dog.speak # => Rex barks!
Abstract Classes
abstract class Shape
abstract def area : Float64
end
class Circle < Shape
def initialize(@radius : Float64); end
def area
Math::PI * @radius * @radius
end
end
← Previous
Crystal Methods — Def, Return Types, Named Arguments and Blocks
Next →
Crystal HTTP Server — Building Web APIs with the Standard Library
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro