13.13. Object Identities

In an earlier example, GraduateStudent and UndergraduateStudent objects were stored in a List collection that had been declared to hold Student objects. An element extracted from the List will be returned as a Student object. The extracted object hasn't "forgotten" what class to which it belongs. An object always retains its class identity; it's simply that we can refer to an object with various different reference variables, which might be declared to be of different types, and it's this phenomenon that affects which messages the compiler believes that an object is capable of responding to. To help illustrate this point, let's use an example.

13.13.1. A Derived Class Object Is a Base Class Object, Too

If we instantiate ...

Get Beginning C# 2008 Objects: From Concept to Code now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.