I was just perusing my copy of Java Design: Building Better Apps and Applets by Peter Coad and Mark Mayfield and noticed a good set of rules to follow when thinking about using inheritance:
- “Is a special kind of”, not “is a role played by a”
- Never needs to transmute to be an object in some other class
- Extends rather than overrides or nullifies superclass
- Does not subclass what is merely a utility class (useful functionality you would like to reuse)
- Within PD(Problem Domain): expresses special kinds of roles, transactions, or devices
Update: I probably should mention that you should fulfill all or at least most of these to consider inheritance. Otherwise use composition or re-analyze your design.
Post a Comment