45 Class inheritance
NamedCounter extends Counter keeps the base fields and adds name. Its constructor calls super(initial: initial) before assigning the new field.
norm
class Counter {
Integer value
Counter(Integer initial) { value = initial }
}
class NamedCounter extends Counter {
String name
NamedCounter(Integer initial, String name) {
super(initial: initial)
this.name = name
}
}
main() {
NamedCounter counter = NamedCounter(initial: 3, name: "visits")
printLine("${counter.name}: ${counter.value}")
}Expected output:
text
visits: 3Try it: Change the initial count and verify the inherited value.
Precise rules: Reference.