Can We Tell What Comes Out?

Here is a picture that might help you visualize the container class problem. Suppose we have a class Ball with two subclasses that represent a BlackBall and a WhiteBall. We have a container, here represented by a box, that can accept and store values of type Ball. When asked, it will randomly select one of its values, and return it as a value of type Ball.

If we insert two balls, one black and one white, and then extract one, can we tell whether or not it is the black ball?

In some languages the answer is yes, we can tell. In other languages it is not possible. We will note these features when towards the end of this chapter we discuss the various different languages describe described in the book.

[audio] [real] Text to accompany slide11, in Chapter 10 of An Introduction to Object-Oriented Programming