posted 22 years ago
It is important to remember that when you cast an instance of an object from one type to another the object instance is not modified in any way. Instead, the only thing that changes is the view of the object. For example, when a subclass such as Dog is cast to the base class, Pet, the view of the Dog instance is narrowed to a Pet base class view. A reference of type Pet can only access the members of the Dog subclass that are declared in the Pet base class.
Of course, the rules of member inheritance and hiding apply as demonstrated by the following code.
Prints: A.s1,B.m1,A.m2
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>