Avoid multiple inheritance
(New page: ''If you have an example of multiple inheritance in your design, assume you have made a mistake and prove otherwise.'' [Riel] Heuristic 6.1) |
m (Reverted edits by Ebybymic (Talk); changed back to last version by Matthew Harward) |
||
(12 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | ''If you have an example of multiple inheritance in your design, assume you have made a mistake and prove otherwise.'' | + | ''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 [[Design by contract|contract]] and indicate a stronger relationship which does not [[Model the real world|model the real world]]. | ||
+ | |||
+ | ''Why is it OK to inherit multiple interfaces, but not multiple classes?'' --[[User:Warwick Irwin|Wal]] (Text above has been clarified to address this.) | ||
+ | |||
+ | == See also == | ||
+ | * [[Riel's heuristics]] | ||
+ | * [[Multiple Inheritance]] | ||
+ | * [[Beware inheritance over composition]] | ||
+ | |||
+ | |||
+ | [[Category:Riel's heuristics]] |
Latest revision as of 03:23, 25 November 2010
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.)