Skip to content

44 Default interface methods ​

label() has a body in Named, so implementations inherit it while supplying only name(). The default calls the implementation through this.

norm
interface Named {
  String name()

  String label() { return "Task: " + this.name() }
}

class Task implements Named {
  String title

  String name() { return title }
}

main() {
  Named item = Task(title: "Learn Norm")
  printLine(item.label())
}

Expected output:

text
Task: Learn Norm

Try it: Add label() to Task and observe which body runs.

Precise rules: Reference.

Norm 0.25