• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception whith Cast ...it's strange

 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody...
I have a problem when i cast a object of a base class ... as follow :
class A {}
class B extends A {}
class C extends A {}
public class Q3ae4 {
public static void main(String args[]) {
A x = new A();
B y = new B();
C z = new C();
x = y; //line 1 it's OK
y = (B) x; //line 2 it's OK
z = (C) x; //line 3 but here,it doesn't work
}
}
My question is : I want to know why when i cast at line 2 i don't have a ClassCastException but when i do it for the line 3 i have one. Nevertheless it's done the same way so for line 3 than at line 2 ? isn't it?
Can you explain me it please.....
Thanks a lot !
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okucu
Lets look at it line by line:
A x = new A();
B y = new B();
The variable x holds a reference to an A object, the variable y holds a reference to a B object.
x = y; //line 1 it's OK
Now the variable x holds a refernce to a B object, this is ok because a B object 'is a' A object.
y = (B) x; //line 2 it's OK
You are casting the reference that x holds to a B object (y was declared to hold a B object reference). This is ok because in the line before this on ewe changed the x variable to point to a B object.
z = (C) x;
Here you are casting the reference in x to a C object. But x doesn't hold a C object it is a B object (or an A object because a B object 'is a' A object).
hope that clears it up for you
 
Engin Okucu
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dave , i thanks a lot, now it clears me up more.
I did understand it.
If i had created a refence to z variable than i would be able to compile whithout problem as follow :
class A {}
class B extends A {}
class C extends A {}
public class Q3ae4 {
public static void main(String args[]) {
A x = new A();
B y = new B();
C z = new C();
x = y;
y = (B) x;
// x=z; ------------ this line
z = (C) x;

}
}
By adding my commentary line (referenced by 'this line' into the program) i can compile. I had completely forget than x was a object of typa A. And to cast it, a reference of the variable x must be created to subclass before casting so that it may work !
Thanks.
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic