Date: Sun, 17 Nov 2002 21:13:36 -0500 (EST) From: Li Liao Subject: Re: casting > > I'm having problem understanding how casting is supposed to work. > Is it almost the same as creating a new object?Then how do we initialize > the fields in this case?To default values as well? > It would be vary helpful to have a detailed procedure description > of how casting is done. About Downcasting: Normally, when you assign an object of a subclass to a variable (or parameter or field) that is expecting an object of a superclass, you lose the ability the access any new fields or methods of the object. For example, consider two classes: class A { int m1() = ...; } class B extends A { int m2() = ...; } Now, suppose we have two variables A a; B b; At some point in the program, we could say a = b; We could then call a.m1(), but we could no longer call a.m2(). In this situation, we know that the actual object in variable a supports the m2 method. The way to regain access to the m2 method is to do a downcast, i.e, ([B]a).m2() This performs a run-time check that the actual object has type B (or a subtype of B). If this check fails, then the program halts with a BadDowncast exception. The downcast is also considered to succeed if the actual value is null. Hope this is helpful. Li