17 Polymorphism: Object Independence

To be most useful, objects need independence. Each should act as though it were a miniature computer that can send and respond to messages. As long as a new object type uses the appropriate interface, you should be able to plug it into existing software.

Literally, polymorphism means “many forms.” On the most basic level, it means the ability to implement the same operation multiple ways. Stated that way, it’s trivial. But the real point is this: without changing anything about the code that uses an object, you can substitute another object type and the program will still work.

Consider an analogy. Your word processor can successfully interact with any kind of printer you purchase. You can even, five years ...

Get C++ Without Fear: A Beginner’s Guide That Makes You Feel Smart now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.