in reply to Overriding methods and Inheritance
Yes. Yes it does. That's the way polymorphism works and is nothing specific to Perl.
What would you expect to happen when you pull an AngryDog's tail? That it would bark (because pull_tail is defined by the base Dog class) or that it would growl (because that's what AngryDogs say)? It's an AngryDog, so it should respond accordingly.package Dog; sub pull_tail { $self->speak; } sub speak { print "Woof!\n"; } package AngryDog; sub speak { print "Grrrrr...\n"; }
(If you wanted all Dogs to react like a base Dog instead of according to their subclass, you could force them to always use the base Dog::speak by changing the $self->speak line to Dog::speak($self), but that's done very rarely, as there are very few cases where it's what you actually want.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Overriding methods and Inheritance
by bamaindk (Initiate) on Jan 08, 2008 at 07:19 UTC | |
by dsheroh (Monsignor) on Jan 08, 2008 at 17:06 UTC |