Avoid multiple inheritance
Line 13: | Line 13: | ||
* [[Riel's heuristics]] | * [[Riel's heuristics]] | ||
* [[Multiple Inheritance]] | * [[Multiple Inheritance]] | ||
+ | * [[Beware inheritance over composition]] | ||
[[Category:Riel's heuristics]] | [[Category:Riel's heuristics]] |
Revision as of 22:18, 28 July 2009
If you have an example of multiple inheritance in your design, assume you have made a mistake and prove otherwise. -- Riel's Heuristic 6.1, Arthur Riel 1996
Explanation
Riel believes that multiple inheritance (MI) is not evil or undesirable; however, it is rarely used correctly. If a situation exists that you believe requires multiple inheritance you should consider this carefully.
Alternatives
Interfaces can often be used instead of multiple superclasses; having a design with multiple superclasses is often an indicator that one of the superclasses should actually be an interface. Extending multiple interfaces means that the class agrees to perform some functionality without saying that the class is one of those objects. For example, implementing the Comparable interface in Java means that your class will support some behaviour enabling it to be compared. It would be a poor design for Comparable to be a class, as subclassing it would go beyond having a contract and indicate a stronger relationship which does not model the real world.
Why is it OK to inherit multiple interfaces, but not multiple classes? --Wal (Text above has been clarified to address this.)