This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference Casting" Watch "Reference Casting" New topic

Reference Casting

farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47
class A{}
class B extends A{}
class C extebds A{}
public class Test{
public static void main(String args[]){
A x=new A();
B y=new B();
C z=new C();
// insert styatement here
1- x=y;
2- z=x;
3- y=(B)x;
4- z=(C)y;
5- y=(A)y;
which one cause runtime exception
Answer is 3 but anyone explain all options kindly
thanks in advance

Regards<BR>Farrukh Mahmud
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
When you encounter such code it's advised to replace A,B and C with real-life things to help understand the relationships among them. I choose that A is Fruit, B is Orange and C is Apple.
Now let's show the relationships: We have that an Orange is a Fruit and an Apple is a Fruit as well.
We create one instance of each, i.e. we have one Fruit instance, one Orange instance and one Apple instance.
Let's look at the different options we have:
1. x=y means that you want to reference an Orange instance with a Fruit reference which is perfectly legal since an Orange is_a Fruit. No runtime exception here.
2. z=x means that you want to reference a Fruit instance with a Apple reference. This will result in a compile-time error since without explicitely casting x to Apple it is not possible and moreover x has to reference an Apple beforehand.
3. y=(Orange)x means that you want your Fruit instance (x) look like it is a Orange and reference it with a variable of type Orange. Well, this will work only if x already references an Orange instance which may not be the case since x may as well reference an Apple instance and you cannot make a Apple look like an Orange. In this case, x references a Fruit and not an Orange. The cast allows to go through compilation without error but at compile time a ClassCastException will be thrown since a Fruit cannot be cast to an Orange.
4. z=(Apple)y means that you want your Orange instance (y) look like it is an Apple and that's is clearly impossible since an Apple and an Orange are not related. This results in a compilation error.
5. y=(Fruit)y means that you want your Orange instance look like it is a Fruit and reference it with a variable of type Orange. The rigth part is not the problem but the result of casting y to Fruit results in y being of type Fruit. Then you want to reference it with a variable of type Orange, which is not permitted by the compiler and results in a compile time error.
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[Blog] [Blogroll] [My Reviews] My Linked In
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by farrukh mahmud:

compiles: because B extends A the variable y 'is a' A
Compiler error: The variable z is to hold a reference to an C object, because x refers to an A object and an A object is not a C object it fails.
Compiles ok becasue of the cast, but will cause a runtime exception because the variable x refers to an A object and can't be cast to a B object.
Compiler error: The compiler knows that the classes B and C are not related so it wont let you cast from one to the other.
Compiler error: The cast of the variable y to an A object is ok becasue a B object 'is a' A object. but the variable y can only hold a B object or one of its subclasses so trying to put an A object into it fails.
hope this helps

Sun Certified Programmer for the Java� 2 Platform

farrukh mahmud
Ranch Hand

Joined: Feb 13, 2001
Posts: 47
thanks guys explanation is good added by knowlwdge
Farrukh Mahmud
I agree. Here's the link:
subject: Reference Casting
It's not a secret anymore!