Course of Raku / Objects, I/O, and exceptions / Classes and objects / Inheritance / Exercises / A shape and a square

Solution: A shape and a square

Here is a possible solution to the task.

Code

class Shape {
    method name {
        'shape';
    }
    method describe {
        'I am a ' ~ self.name;
    }
}

class Square is Shape {
    method name {
        'square';
    }
}

say Square.new.describe;

🦋 You can find the source code in the file shape-and-square.raku.

Output

I am a square

Comments

  1. Square inherits describe from Shape and overrides only name.

  2. describe calls self.name, which picks up the overriding name, so the description says square rather than shape.

Course navigation

A shape and a square   |   An inherited attribute