aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Downcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Downcasting" Watch "Downcasting" New topic
Author

Downcasting

Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Marcus Green Mock exam:
What will happen if you attempt to compile and run the following code?
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception

class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
The given answer is (3)
But I think is should be (1). Casting down the object hierarchy as the compiler cannot be sure what has been implemented in descendent classes. so at run time, it might turn out to be wrong casting and might give exception.
But otherwise, isn't the above code correct where downcasting is implemeted??
Sam Zheng
Ranch Hand

Joined: Nov 29, 2000
Posts: 61
Actually you have answered your own question perfectly. At
compile time, everyone involved is happy. At run time, it
turns out that this downcasting is not valid, so a
ClassCastException is thrown.
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Actually, the answer is there 'might' be a possibility that at run time the downcasting might give runtime exception. But not always. (if the casting is correct).
So isn't that question ambiguous. Should I select (1) or (3)
quan zhu
Greenhorn

Joined: Dec 14, 2000
Posts: 27
When I try it with JBuilder 4, I got the compile time error:
"variable sub might not have been initialized at line..."
Should this be the standard?
tony hutcheson
Ranch Hand

Joined: Dec 08, 2000
Posts: 38
I believe that the answer is 3) because you can actually assign an object of the Sub class to the Base class:
e.g. Base b = new Sub();
Which would make
Sub s = (Sub) b ;
valid.
Since it is possible to cast the objects in this manner, the compiler doesn't have a problem with it. In your example it can't be determined until runtime that the object(b) can't be cast to the Sub class.
quan zhu
Greenhorn

Joined: Dec 14, 2000
Posts: 27
sorry, my mistake.
Originally posted by quan zhu:
When I try it with JBuilder 4, I got the compile time error:
"variable sub might not have been initialized at line..."
Should this be the standard?

Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
I got it. But the above code would have been correct if written like:
Base b=new Base();
Sub s=new Sub();
s=(Sub)b;
sachin patel
Ranch Hand

Joined: Nov 28, 2000
Posts: 75
No the above code is not correct. it would be correct like this
Base b= new Sub();
Sub s=(Sub)b;


Sachin,<P>****************************************************<BR>Learn from others mistakes. Life is too short to make all yourself.<BR>****************************************************
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
But isn't Sub the subclass of Base;
Why isn't the following code correct, I thought it is downcasting;

Base b=new Base();
Sub s = new Sub();
s=(Sub)b;
If this is incorrect, then what is downcasting?
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
That is not correct because in order for downcasting, it has to first be a Sub. When you create an object, Java knows what the object is. You can upcast it and Java will allow for polymorphism and at runtime it will call the correct methods. But, Java still knows what the object is. So "the class you are casting to must be the original class of the object, or must be a superclass of the object" (Ivor Horton - Beginning Java 2).
So what that all means is that, if you say:
Base b = new Sub();
You are really upcasting Sub to a Base. Since it was originally a Sub, you can cast it back down and everything will run fine when you say:
Sub s = Sub(b);
But you do have to have the explicit cast because you are downcasting.
Now for your example:
Base b = new Base();
Now the original object is a Base, so you can't downcast it to a Sub becuase it never was a Sub to begin with.
Hope that makes some sense as this can be a bit confusing.
Bill
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Thanks for the explanation bill. Thanks to all of you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Downcasting