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

Heterogeneous Collections

Because any value can be stored in an Object, this allows heterogeneous collections; although the actual type must be checked when a value is removed:
Stack stk = new Stack();
stk.addElement(new Cat());
stk.addElement(new Dog()); 
	// ... adding more values
	// now do something with Cat values
if (stk.peek() instanceof Cat) {
		// do conversion to Cat
	Cat aCat = (Cat) stk.pop(); 
	// .. also do something with cat values
	// now do something with Dog values
} else if (stk.peek() instanceof Dog) {
		// do conversion to Dog
	Dog aDog = (Dog) stk.pop(); 
	// .. do something with Dog value
Intro OOP, Chapter 19, Slide 06