Inheritance
A class inherits from another using the extends
keyword.
// Base class
class Animal(var String name)
{
pub fun say_name()
{
print($name)
}
}
// Child class
class Cat(String name, Int lives)
extends Animal(name) {}
Cat("Michi", 9).say_name()
thp
The call to the parent constructor is done right there, after the parent class name.
class Dog(String name)
extends Animal(name) {}
// |----|
// This is the call to the parent constructor
thp
You must always call super, even if the parent doesn’t define any parameters:
class Parent() {}
class Child()
extends Parent() {}
thp