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 squareComments
SquareinheritsdescribefromShapeand overrides onlyname.describecallsself.name, which picks up the overridingname, so the description sayssquarerather thanshape.