Polymorphism encourages encapsulation, but absolutely requires inheritance.
Completely false.

? Not just "false" but "completely false"? So polymorphism neither encourages encapsulation nor requires inheritance?

This is not polymorphism, this is overloaded functions, as in multimethods of perl 6. Your methods are, more or less:

sub quack(RubberDuck $self) { ... } sub quack(Mallard $self) { ... }
This is not polymorphism. Overloadable functions (same name, different contents) are usually found only in OO languages, but I don't think that this is causal. I think C may have overloadable functions in the not-too-distant future, if it doesn't already in the latest spec.

package RubberDuck; sub new { bless {}, shift } sub make_noise { print "Plug yer ears!\n"; $_[0]->quack(); } sub quack { print "Squeak!\n" } sub DESTROY { print "RubberDuck goes back in the toybox.\n" } package Mallard; our @ISA = qw(RubberDuck); sub new { bless {}, shift } sub quack { print "Quack!\n" } sub DESTROY { print "Mallard flies away.\n" } package main; for my $duck_class (qw( RubberDuck Mallard )) { my $duck = $duck_class->new(); $duck->make_noise(); }

This is polymorphism. The parent class calls a function in the parent class, but the derived class gets to intercept it and do stuff with it. That stuff may include calling the parent class, or, as in this example, it may not.


In reply to Re^4: Understanding 'Multiple Inheritance' by Tanktalus
in thread Understanding 'Multiple Inheritance' by punkish

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.