docs / Getting Started / Classes

Classes

Classes are reference types with constructors, methods, visibility modifiers, and single inheritance. Method calls on base-typed variables are dispatched at runtime via vtables.

use system.io

abstract class Shape
    abstract fn area() float

class Circle extends Shape
    private radius float

    fn init(r float)
        self.radius = r

    fn area() float
        3.14159 * self.radius * self.radius

class Rectangle extends Shape
    private width float
    private height float

    fn init(w float, h float)
        self.width = w
        self.height = h

    fn area() float
        self.width * self.height

fn main()
    let c = Circle(r: 5.0)
    println(f"{c.area()}")       // 78.53975

    // Virtual dispatch — method resolved at runtime
    let s Shape = Circle(r: 3.0)
    println(f"{s.area()}")       // 28.27431

Key concepts

  • init — Constructor method. Fields are initialized via self.field = value. Instantiation uses named arguments matching init parameters.
  • extends — Single inheritance. Subclasses inherit all fields and methods.
  • super.method() — Calls the parent class implementation. super.init() chains to the parent constructor.
  • abstract — Abstract classes cannot be instantiated. Abstract methods must be overridden in concrete subclasses.
  • Virtual dispatch — When a variable is typed as a base class, method calls are resolved at runtime to the correct subclass implementation.

Visibility modifiers

ModifierAccessible from
publicEverywhere (default for methods)
protectedDeclaring class and all subclasses
privateDeclaring class only