June 2025
Intermediate to advanced
1093 pages
33h 24m
English
Having said that, I'll steer back to the main point: in situations where you might forgo a seemingly logical inheritance hierarchy, you can, on the other hand, use inheritance to consolidate unrelated data and avoid code duplication. As I said at the beginning, I call this the technical reason for an inheritance hierarchy.
Examine Chapter 12, Listing 12.24 and Listing 12.27, for example. If you write out the data types Month and Day completely like Year, then you have a lot of code that looks identical in three classes. Code duplication is bad—and you can build a small inheritance hierarchy to avoid it.
First, in Listing 15.1, you define the common ancestor Value of the three helper classes.
Read now
Unlock full access