The Container Problem

In an earlier slide I said that the reverse polymorphism problem was surprizingly common. In large part, this is due to the connection to the container problem.

Container classes are classes used to hold a collection of values. Examples are the common data structures, such as arrays, lists, trees, hash tables, and so on.

In many languages, when we place a value into a container, it is stored in a variable of a more general class. For example, in Java collections typically store their values in variables of type Object.

When we pull a value out of a container, we must then restore its original type. But, how can we determine the original type?

Solving this problem generally requires that values must keep track of their own type. In some languages they do, and in other languages, they do not.

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