Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/extending-classes-and-interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.