Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: It's a dog, but what kind? (polymorphism , in Perl OO)

by hardburn (Abbot)
on Mar 23, 2004 at 21:04 UTC ( #339191=note: print w/ replies, xml ) Need Help??


in reply to It's a dog, but what kind? (polymorphism , in Perl OO)

$self= new $breed; . . . bless $self,$breed;

I don't think this will do what you want. This will re-bless the reference into the dog package, which means that if you asked for a 'cocker' breed, any methods that dog::cocker overrides won't be called. Instead, the methods in dog will be called. Update: Read the code wrong--ignore this pargraph.

Which is really unfortunate, since it could be quite useful to override the bark method in your final example. Instead of checking if this instance has a voice attribute that is true, the subclass would already know and Do The Right Thing. That's where polymorphism starts becoming powerful.

----
: () { :|:& };:

Note: All code is untested, unless otherwise stated


Comment on Re: It's a dog, but what kind? (polymorphism , in Perl OO)
Select or Download Code
Re: Re: It's a dog, but what kind? (polymorphism , in Perl OO)
by blue_cowdawg (Monsignor) on Mar 23, 2004 at 21:30 UTC

        I don't think this will do what you want.

    Actually it does. The code was actually tested before I posted it. What you see is what I got.


    Peter L. Berghold -- Unix Professional
    Peter at Berghold dot Net
       Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

      I see where I went wrong. I read the above as bless $self, $class instead of bless $self, $breed.

      In any case, I still think that if you want this to be a good tutorial on polymorphism, some of the subclasses should override bark. As it is, it doesn't take advantage of polymorphism.

      ----
      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

            good tutorial on polymorphism, some of the subclasses should override bark.

        DOH! I meant to and forgot! Actually the subclass cocker was going to override "wag_tail" since they have short tails more or less wiggle their hind ends instead. ;-)


        Peter L. Berghold -- Unix Professional
        Peter at Berghold dot Net
           Dog trainer, dog agility exhibitor, brewer of fine Belgian style ales. Happiness is a warm, tired, contented dog curled up at your side and a good Belgian ale in your chalice.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://339191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-12-29 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (183 votes), past polls