In Perl, if you have class X with methods 'foo' and 'bar',
and class Y which inherits from X, but has a method 'foo',
then the following takes place:
package X;
sub foo;
sub bar;
package Y;
@ISA = 'X';
sub foo;
package main;
X->foo; # calls X::foo('X');
X->bar; # calls X::bar('X');
Y->foo; # calls Y::foo('Y');
Y->bar; # calls X::bar('Y');
In C++, you would have to declare X::foo() to be
virtual:
// yes, this is C++, so shoot me
class X {
public:
virtual int foo () { return 1; }
int bar () { return 2; }
};
class Y : public X {
virtual int foo () { return 3; }
};
You don't need to do that in Perl, since an object's method
search tree starts in the object's class (Y, in this case).
Since an object in Y inherits from X, then Y->isa(X)
is true, just like you can write a function in C++ like:
void printStuff (X& obj);
and it will accept an object of class Y (because of the rules
of inheritance). In Perl, you can do this checking via the
isa() method shown above.
As for ensuring an abstract base class never has objects of
IT specifically -- only of it's sub-classes -- you can't
stop a person from brute-force bless($obj,'Employee'),
but you can make Employee::new die if its first
argument is 'Employee':
package Employee;
sub new {
my ($class,$fname,$lname) = @_;
die if $class eq 'Employee';
bless { FNAME => $fname, LNAME => $lname }, $class;
}
package Employee::Boss;
@ISA = qw( Employee );
sub new {
my ($class,$fname,$lname) = @_;
my $self = $class->SUPER::new($fname,$lname);
$self->{PAY} = 1_000_000;
bless $self, $class;
}
# and so on...
And then a program would run like:
use Employee;
# defines Employee, Employee::Boss,
# Employee::Hourly, and Employee::Intern
$joe = Employee::Intern->new('Joe', 'Schmoe');
$jay = Employee::Boss->new('Jay','Schmay');
$not = Employee->new('Not', 'Happening'); # <-- dies
$_="goto+F.print+chop;\n=yhpaj";F1:eval |