Features to Note

Here are some more features to note in relation to this class. First, notice that the values are held in a variable that is declared as type Object. This is one aspect of polymorphism, a topic we will discuss in detail in later chapters. Recall the principle of substitutability; a variable declared as a parent class can, in fact, hold values from a child class. Since Object is parent to all classes, a variable declared as Object can hold any object type.

However, when a value is pulled out of the list it is only known to be an Object. It must therefore be cast to the appropriate type.

Note that the class Object is not quite universal -- primitive types, such as integers and reals, are not considered to be Objects in Java, and so cannot be held in containers. For just this reason Java provides a number of wrapper classes for these values.

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