From 9d2ea672c51abee794788d5063150f259b50e290 Mon Sep 17 00:00:00 2001 From: Magnus Madsen Date: Wed, 25 Feb 2026 14:09:19 +0100 Subject: [PATCH] docs: document super method calls in anonymous subclasses Add a "Calling Super Methods" section to the Classes and Interfaces page explaining the super.methodName(args) syntax for calling parent class method implementations when overriding methods in new expressions. Co-Authored-By: Claude Opus 4.6 --- src/extending-classes-and-interfaces.md | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/extending-classes-and-interfaces.md b/src/extending-classes-and-interfaces.md index 7c91fc9c..f5d2fc29 100644 --- a/src/extending-classes-and-interfaces.md +++ b/src/extending-classes-and-interfaces.md @@ -40,3 +40,31 @@ def newObject(): Object \ IO = new Object { def toString(_this: Object): String = "Hello World!" } ``` + +## Calling Super Methods + +When overriding a method in an anonymous subclass, we can call the parent +class's implementation using `super.methodName(args)`. + +For example, we can extend `Thread` and override `toString` to include the +parent's default representation: + +```flix +import java.lang.Thread + +def main(): Unit \ IO = + let t = new Thread { + def new(): Thread \ IO = super("my-thread") + def run(_this: Thread): Unit \ IO = + println("Hello from ${Thread.currentThread().getName()}") + def toString(_this: Thread): String \ IO = + "MyThread(" + super.toString() + ")" + }; + println(t) +``` + +Here `super.toString()` calls the `toString` method defined by `Thread` (the +parent class), and we wrap the result in `"MyThread(...)"`. + +> **Note:** Super method calls can only be used inside `new` expressions, i.e. +> when defining an anonymous subclass.