Local database reading edition
Why/How does casting work?
Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.
Forum index · Java - Tips Of The Day
Opening post
Asha · 2001-04-27 18:29:43
First, you can't cast an object to another object in Java. Casting effects expressions and reference variables, not objects. An explicit cast operation signals the compiler that you want to change the <b>type</b> of an expression or reference variable. You may think, "Well, Java is an object-oriented language, so what's wrong with that?" The problem is often a type-oriented perspective is needed.
<b><u>Don't think in terms of objects, think in terms of type.</u></b>
The expression someCollection.get( i ) has type Object. But the reference variable myObject has type MyType. So you need to cast the right-hand-side expression to the expected type of the left-hand-side expression.
Both compile time and runtime casting utilize the Java type system. The real issue arises when you realize that in Java there are two kinds of inheritance: implemetation (or class) inheritance and type inheritance. Casting relies on the type hierarchy defined by type inheritance.
Since all runtime objects are rooted from java.lang.Object, you are always guaranteed that attaching an Object type reference to any object will succeed.
For example, container classes for Java are written in terms of the Object type. You add an object to the container using a method call such as add( myObject ), where the reference variable myObject may be attached to any Java object. For our purposes, let's suppose the reference variable myObject has type MyType. But the actual definition of the container method is something like add(Object), that is, the method expects to receive a reference of type Object, not MyType. In this case, the compiler implicitly casts your reference variable myObject to the type Object. Again, this cast is guaranteed to work.
Now, when you wish to retreive an object from the collection, say you use a method like get(int) that returns the ith object in the collection. But if you look at the definition of the method get(int), you'll see it returns an Object type reference, not a MyType type reference. Remember, the container class only deals in Object types. So to "retrieve" the functionality of the underlying object, you explicitly cast the returned reference to MyType using a line of code like:
<pre id=code><font face=courier size=2 id=code>MyType myObject = (MyType)someCollection.get( i ); </font id=code></pre id=code>
Note that so far I haven't mentioned the underlying object. someCollection.get( i ) returns a reference which points to some runtime object. That runtime object has some type as well. (Strictly speaking, an object does not have a type, but rather has a class. But every class defines one type, and by object type I mean the type defined by the class of that object.) So at runtime, the cast (MyType)someCollection.get( i ) will require attaching a reference of type MyType to the underlying object. The runtime system will verify the validity of that cast, and will throw a ClassCastException if it is not permissible. Most casting is verified at compile time, but in an example like this, the compiler can't know what type object will be reference by the call someCollection.get( i ). So the cast must be verified at runtime.
Asha Balasubramanyan
http://www.ammu.org
Source notes
MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 143