Introduction to OOP Chapter 18: Generics : next previous audio real text

Inheritance and Generics

Remember the class Box. Suppose a class Person has subclasses BoyChild and GirlChild. What is the relationship between Box[Person] and Box[BoyChild]?

Unfortunately, runs into problems with the principle of substitution.

Assume Box[BoyChild] is a subclass of Box[Person], would make the following legal:

Box[Person] aBox = new  Box[BoyChild];
Person aGirl = new GirlChild;
aBox.set(aGirl);
A similar argument can be made for the reverse.
Intro OOP, Chapter 18, Slide 07