7. Role of Inheritance and Polymorphism with Numeric Types

7.1. Review of example classes

Consider the following numeric types from earlier chapters:

Complex

Rational

Money

Mass

Days

Date

Voltage

Current

Resistance

Power

Which ones lend themselves to specialization, that is, to being used as a base class for one or more specialized derived types?

The surprising—and to some, disappointing—answer is that none of them do. Even though some aspects of object orientation are well suited to elementary data types, inheritance and polymorphism are less useful with elementary objects than with composite or container objects.

7.2. Representation is not specialization

Suppose someone proposes this inheritance hierarchy:

Or this: ...

Get Object-Oriented Computation in C++ and Java: A Practical Guide to Design Patterns for Object-Oriented Computing now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.