Forums Register Login

Casting Doubt

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.....
+Pie Number of slices to send: Send
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;
}
}
+Pie Number of slices to send: Send
Thanks a ton Anuj & Pranav.

You gave a very detailed explaination.
brevity is the soul of wit - shakepeare. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 787 times.
Similar Threads
doubt
InstanceOf Doubt.
clear my doubt
Another examLab casting question
doubt in casting
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:31:50.