File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Casting up thie hierarchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting up thie hierarchy" Watch "Casting up thie hierarchy" New topic

Casting up thie hierarchy

Richard Grey

Joined: Nov 21, 2001
Posts: 25
According to the Java Language Specification, you can cast an object to another object higher in the inheritance heirarchy.
For example, the following is valid and displays 1:

However, the following compiles ok, but at runtime gives a ClassCastException:

Or is this an obvious question ?
Sam Baloch

Joined: Apr 10, 2002
Posts: 2
if a variable of upper level class receives the reference of a lower level object then it can get type casted to the lower level objet type.
for example; the following code is not correct

try out this code.

[ April 11, 2002: Message edited by: Sam Baloch ]

[Smart Rancher]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
When you do this type of thing in your code it is always helpful to keep in mind the 'is a' and 'has a' phrases. In your first example lets use the 'is a' phrase and look at it line by line.
// create an Integer
Integer i1 = new Integer("1");
/* assign a variable 'o' to refer to the new Integer object. Even though o is supposed to refer to an Object object you can have it refer to an Integer Object because an Integer is a Object. */
Object o = i1;
/* create a new Integer varaiable i2 and have it refer to the object that o refered to but cast to an Integer. The variable o refers to an object of type Object so without the cast you would get a reference to an Object. The cast works because in the previous line we made o refer to an Integer object, so we can cast back to that same object. */
Integer i2 = (Integer) o;
// print the object refered to by i1
Now lets look at the second example you posted:
// create a new java.util.Date object
java.util.Date ud = new java.util.Date();
/* The compiler will let you cast this and it will accept it because the actual types will not be checked until runtime. At runtime however, it will fail because you are trying to cast a java.util.Date into a java.sql.Date object and a util.Date is not a sql.Date. */
java.sql.Date sd = (java.sql.Date) ud;
Hope that helped
[ April 11, 2002: Message edited by: Dave Vick ]

Carl Trusiak

Joined: Jun 13, 2000
Posts: 3340
You need to rember one thing about casting. It only changes the reference type to the Object not the Object. You can cast up until you reach the Object itself and then no further. The example you gave has a tree of
Object - java.util.Date - java.sql.Date
So, you can do a
Object o = new java.util.Date()
java.util.Date d = (java.util.Date)o;
Going up the tree but not passing the actual object. But,
Object o = new java.util.Date();
java.sql.Date d = (java.sql.Date)o;
is illegal because it goes further up the tree than the actual Object.
[ April 11, 2002: Message edited by: Carl Trusiak ]

I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
I agree. Here's the link:
subject: Casting up thie hierarchy
It's not a secret anymore!