Course of Raku / Objects, I/O, and exceptions / Classes and objects / Inheritance / Exercises / Employees and roles

Solution: Employees and roles

Here is a possible solution to the task.

Code

class Employee {
    has $.name;
    method role {
        'staff';
    }
    method badge {
        "$.name - " ~ self.role;
    }
}

class Manager is Employee {
    method role { 'manager' }
}

class Intern is Employee {
    method role { 'intern' }
}

say Manager.new(name => 'Anna').badge;
say Intern.new(name => 'Bob').badge;

🦋 You can find the source code in the file dog-and-cat.raku.

Output

Anna - manager
Bob - intern

Comments

  1. Manager and Intern inherit the name attribute and the badge method from Employee, so neither of them needs to repeat that code.

  2. Each child overrides role. Because badge calls self.role, it picks up the overriding version belonging to the actual object, giving a different badge for the manager and the intern.

  3. The name of the method, role, is just a name. But it coincides with the keyword role that will be introduced later.

Course navigation

Employees and roles   |   A shape and a square