14.6. Multiple InheritanceHow does Perl wander through the @ISA TRee? The answer may be simple or complex. If we don't have multiple inheritance (that is, if no @ISA has more than one element), it is simple: Perl simply goes from one @ISA to the next until it finds the ultimate base class whose @ISA is empty. Multiple inheritance is more complex. It occurs when a class's @ISA has more than one element. For example, suppose we have a class called Racer, which has the basic abilities for anything that can race, so that it's ready to be the base class for a runner, a fast car, or a racing turtle. With that, we could make the RaceHorse class as simply as this:[*]
{ package RaceHorse; our @ISA = qw{ Horse Racer }; } Now a RaceHorse can do anything a Horse can do, and anything a Racer can do as well. When Perl searches for a method that's not provided directly by RaceHorse, it first searches through all the capabilities of the Horse (including all its parent classes, such as Animal). When the Horse possibilities are exhausted, Perl turns to see whether Racer (or one of its subclasses) supplies the needed method. On the other hand, if we want Perl to search Racer and its subclasses before searching Horse, we can put them into @ISA in that order (see Figure 14-1). Figure 14-1. A class may not need to implement any methods of its own if it inherits everything it needs from its parent classes through multiple inheritance |