Your base class should not really have to guess all the future ways in which derived child classes can screw things up :) in the base class, implement the method you want:

package Parent; sub X { my $self = shift @_; print "called Parent::X()\n"; ... } # ------------------- # and in the Child class you specialise this package Child; use base qw(Parent) sub X { my $self = shift @_; $self->SUPER::X(@_); # Call Base class method # then do some extra stuff print "called Child::X()\n"; ... } # ------------------- # and further down the chain... package GrandChild; use base qw(Child) sub X { my $self = shift @_; $self->SUPER::X(@_); # Call up one level # then do some extra grandchild stuff print "called GrandChild::X()\n"; ... }
This works to any depth of inheritance. One of the main benefits of OO is this type of inheritance where you can specialise a child class without having to recode the Parent and causing unanticipated issues elsewhere.

So in my opinion, _Private_X should be court marshalled. I usually find that $obj->can('method') is more suited for use in handling collections of different types of objects. If you are using it for an inheritance hierarchy trick, look again - its probably a mistake.

Regards,

Jeff


In reply to Re: overridden method - best way by jaa
in thread overridden method - best way by shemp

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.