6.3. Collections of Supertypes
We said earlier that arrays, as simple collections, contain items (either simple types or objects) that are all of the same type: all int(egers), for example, or all (references to) Student objects. However, the power of inheritance can be used to make arrays more versatile in the types of objects that they can hold.
If we declare an array to hold objects of a given type (for example, Person), we're free to insert objects explicitly declared to be of type Person or of any types derived from Person (for example, UndergraduateStudent, GraduateStudent, and Professor). This is because of the "is a" nature of inheritance; UndergraduateStudent, GraduateStudent, and Professor objects, as subclasses of Person, are simply ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access