• Post Reply Bookmark Topic Watch Topic
  • New Topic

Class cast  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Firstly, could you read the code bellow.

1: class A{ int x;}
2: class B extends A{}
3: class Casting{
4: public static void main(String args[]){
5: A objA = new A();
6: B objB = new B();
7:A temp = objB;
8:B test1 = (B)temp;
9:B test2 = (B)objA;
10:}
11:}

I understand that the line 9 will compile but will not run since objA previously is a parent class A - cannot cast a class which was not a subclass to subclass type. But why is line 8 possible to run?

I`ve figured that you can assign child class B to parent class A but not vice versa. And you can call parent method methodA from child object objB but not vice versa. But I dont understand the part "you cannot assign parent class to a child class." Please someone explain this to me!!
 
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes 9 should throw a ClassCastException. 8 will compile and run fine because...

think about this - a parent class is pointing to the child class, thats fine. since the parent reference contains a child object anyway , it can be converted into a child referece pointing to a child object. that is what u did in line 8.

in line 9 parent reference contains a parent object. so it wont compile since it cant convert it into a child object. thats what you are asking it to do. " you say to the compiler " convert this parent into a child please " and the compiler says "sorry. no can do !"
 
mi te
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your post. Yes, I understand what you are saying. But according to the book I`m reading, it is possible to cast a class which used to be a child class type back to child class, but not otherwise. So I understand that line 9 is trying to cast a class that has always been parent class into child class, in which there will be an error at run time. But in line 8 the class it is casting seems to be a parent class anyway to me, since the line 7 saids A temp = objB. Does line 7 mean that temp is now a child class B? Please make this clear for me!!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this example might help:



That clear it up?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What line 7 means is that a parent reference is pointing to a child object. Actually that code does it indirectly. objB is pointing to object B , so when u point A temp = objB it points to object B.
 
mi te
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i got it now! Thank you everyone.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!