O'Reilly logo

Object-Oriented Computation in C++ and Java: A Practical Guide to Design Patterns for Object-Oriented Computing by Conrad Weisert

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required