Class Hierarchy Design Considerations for Extensibility
Even well-designed class hierarchies need to evolve over time. The early choices you make when designing a class hierarchy can simplify your work later.
Extending Class Hierarchies
The following list contains suggestions for making your class hierarchies easier to extend:
Class hierarchies are defined from the general to the specific. Define classes to be as general as possible at each level of an inheritance hierarchy. Derived classes can inherit, reuse, and extend methods from base classes.
Suppose, for example, you are designing a class hierarchy that models computer hardware. When you start modeling output devices you could define classes named
Display
,Printer
, andFile
. You could then define the classes that implement the methods defined in the base classes. For example, theLCDDisplay
class could be derived fromDisplay
and implement a method namedEnterPowerSaveMode
.Be generous in defining data types and storage to avoid difficult changes later on. For example, you might consider using a variable of type Long even though your current data may only require a standard Integer variable.
Only expose items that are needed by derived classes. Private fields and methods reduce naming conflicts and protect other users from using items that you may need to later change.
Members that are only needed by derived classes should be marked as Protected. This ensures that only the derived classes are dependent on these members and makes it easier to update these members during development.
Make sure that base class methods do not depend on Overridable members, whose functionality can be changed by inheriting classes.
See Also
Reference
Concepts
Considerations for Choosing Access Levels for Methods
Base Class Design Changes After Deployment