Perl

How can you call a method of an inherited class?

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

# Class `A` with `printA` method.
package A;

sub new { return bless {}, shift; };
sub printA { print "A"; };

# Class `B` that extends or use the parent class `A`.
package B;

use parent -norequire, 'A';

sub new { return bless {}, shift; };

# Instance class `B` allows call the inherited method
my $b = B->new();
$b->printA();