in reply to Re: Overriding methods and Inheritance
in thread Overriding methods and Inheritance

Yes, I can see and understand that logic. I just didn't think that the class I inherited from would know anything about a child. Statically it does not but runtime what is being passed around is as you said an "AngryDog". I was/am confusing static model with the dynamic one.

So for this to work I need to "morph" AngryDog to a Dog prior to asking SUPER to handle. It might even be reasonable to expect "$self->SUPER" to do this.

I think I might also have selected the wrong mechanism, as stated in other reply to my question. Delegation seems maybe the better solution. I must admit though it sure did look like the right candidate for inheritance.

I wonder what happens in this scenario in C++?

Anyways, thanks for the help:-)

  • Comment on Re^2: Overriding methods and Inheritance

Replies are listed 'Best First'.
Re^3: Overriding methods and Inheritance
by dsheroh (Monsignor) on Jan 08, 2008 at 17:06 UTC
    It might even be reasonable to expect "$self->SUPER" to do this.

    Just to extend my example a little to include that, let's add another method to AngryDog:

    sub pull_tail { $self->SUPER; bite(); }
    In real-world logic, it's obvious that the AngryDog should still growl before biting when its tail is pulled, not bark.

    It's been quite a while since I last used C++, but I talked to a Java junkie friend the last time something like this came up and he confirmed that Java handles it the same as Perl does. I'm pretty sure C++ (and, for that matter, any language that supports polymorphism) would also behave similarly. To do otherwise would render abstract classes/methods pointless. (Derived class overrides the abstract method, base class method calls the abstract method, and... either nothing happens or an exception is thrown unless it calls the derived class's version instead of the base version.)

    But, yeah, I do agree that inheritance looks like an obvious candidate, but dragonchild's AUTOLOAD-based solution is surely both the slickest solution and the one which requires the least code to do what you want.