Inheritance Polymorphism

To print labels for your CD collection, you might design a one-size-fits-all label that would work for any CD, but then design labels for specific genres that printed extra information more appropriate to those genres.

    $cd    = CD::Music->new(...);
    $cdc   = CD::Music::Classical->new(...);
    $cdj   = CD::Music::Jazz->new(...);
    $cd->print_label();     # would work for any CD
    $cdc->print_label();    # would work for classical CDs
    $cdj->print_label();    # would work for jazz CDs

Inheritance polymorphism requires that objects provide a specific method and that they belong to classes in a common hierarchy.

We re-use the interface and we re-use the code in $cd->print_label() if our invoking class does not define its own print_label() method.

Previous Back to start of show Next
Slide: 42 inheritance_polymorphism © 2003 James E. Keenan