Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting Doubt

 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Program 1

class AA
{
}

class BB extends AA
{
}

class casting
{
public static void main(String arg[])
{
AA a=new AA();
BB b=new BB();
AA c=new BB();
b = (BB) a; //line 1
a = b ;
b = (BB) c;
c = b;
}
}

when I run this program it is compiling fine,but throwing an exception stating that "AA cant not be cast to BB"


Look at another one:

Program 2

class parent
{
}

class castingTest extends parent
{
public static void main(String ar[])
{

parent p=new parent();

castingTest c=new castingTest();

p=c;
c=(castingTest)p;//Line A
}

}

Where as this is not causing any problem.

My doubt is in program 1 above,line 1 is not a problem,as it is downcasting,casting is done properly when assigning a reference of superclass to a subclass reference.

But why it is throwing a runtime exception?

Why it is not the case with second program?

In this "Line A" too doing the same(Down casting)but running without any Exception.


Please clear my doubt
 
Anuj Kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the second program when you are coding like this...
parent p=new parent();

castingTest c=new castingTest();

p=c;
c=(castingTest)p;//Line A
then p=c causes reference variable p to refer to object of type castingTest and when you are typecasting in Line A then it checks for the runtime type of the object referred by the reference variable which is of type castingTest therefore there is no problem.....
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi vatsalya,

My doubt is in program 1 above,line 1 is not a problem,as it is downcasting,casting is done properly when assigning a reference of superclass to a subclass reference.

But why it is throwing a runtime exception?

Why it is not the case with second program?

In this "Line A" too doing the same(Down casting)but running without any Exception.


Your 1st program is not giving any runtime exception cause compiler only knows that the classes AA and BB are from same inheritence tree so can be mutually casted. But when you run it, the line 1 b = (BB) a; //line 1 is casting a to b. But you are not sure that a is of type of b, so it gives you a classcast exception. Cause a super class can have many subclasses.While in your second program you have given p=c; which states that p is of type c so it get running.So as to cast this you have to be sure that a has to be type of b. In such cases its better to do instanceof check as follows-:


class AA {
}

class BB extends AA {
}

class casting {
public static void main(String arg[]) {
AA a = new AA();
BB b = new BB();
AA c = new BB();// line 1
if (a instanceof BB) {
b = (BB) a;
System.out.println("Before ");
}// if a is a BB
a = b;
if (a instanceof BB) {
b = (BB) a;
System.out.println("After ");
}// if a is a BB
a = b;
b = (BB) c; // Here it is fine as c is a type of BB, in line 1
c = b;
}
}
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a ton Anuj & Pranav.

You gave a very detailed explaination.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic