home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Learning Perl Objects, References & ModulesLearning Perl Objects, References & ModulesSearch this book

11.6. Multiple Inheritance

How does Perl wander through the @ISA tree? The answer may be simple or complex. If you 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 someone had given an existing 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, you can make the RaceHorse class as simply as this, maybe:[58]

[58]If there is a conflict among the methods of Horse and Racer, or if their implementations aren't able to work together, the situation can become much more difficult.

{
  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 you want Perl to search Racer and its subclasses before searching Horse, put them into @ISA in that order (see Figure 11-1).

Figure 11-1

Figure 11-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



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.