Introduction to OOP Chapter 19: Container Classes : next previous audio real text

Using Substitution and Downcasting

In Java and other languages, (almost) all values can be stored in a variable of type Object.

Therefore, we write containers that store Object values.

Problem. Requires a cast when a value is removed from the container.

Vector aVector = new Vector();
Cat Felice = new Cat();
aVector.addElement(Felice);
	...
	// cast used to convert Object value to Cat
Cat animal = (Cat) aVector.elementAt(0);
Worse problem, typing errors are detected when a value is removed from the collection, not when it is inserted.
Intro OOP, Chapter 19, Slide 05