...

/

Solution Review: Implement Inheritance

Solution Review: Implement Inheritance

See the solution to the exercise on creating two child classes for a parent class using Moose.

We'll cover the following...

Solution

Let’s look at the solution before jumping into the explanation:

Press + to interact
package Shape {
use Moose;
sub area {
die "area method must be overridden";
}
}
package Rectangle {
use Moose;
extends 'Shape';
has 'length' => (is => 'rw', isa => 'Num');
has 'width' => (is => 'rw', isa => 'Num');
sub area {
my $self = shift;
return $self->length * $self->width;
}
}
package Circle {
use Moose;
extends 'Shape';
has 'radius' => (is => 'rw', isa => 'Num');
sub area {
my $self = shift;
return 3.1416 * ($self->radius ** 2);
}
}

Explanation

Let's go through the ...