The super keyword
In Chapter 4, we learned all about inheritance and how we can override functions from a base class in an inherited class. This overriding replaces the function with a whole new body and discards the original implementation of the base class. However, sometimes, we still would like to execute the original logic that was defined in the parent class.
To accomplish this, we can use the super
keyword. This keyword gives us direct access to all the functions of the parent class on which the current class was based. Consider the following example, where we want to have different kinds of arrows in our game to shoot enemies with:
class BaseArrow: func describe_damage(): print("Pierces a person") class FireArrow extends BaseArrow: func describe_damage(): super() print("And sets them ablaze")
Here, we define...