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

In reply to RE: Re: Best Method of Object-Oriented Behavior Addition? by japhy
in thread Best Method of Object-Oriented Behavior Addition? by princepawn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.