File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting Doubt" Watch "Casting Doubt" New topic
Author

Casting Doubt

vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
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

Joined: Jan 02, 2008
Posts: 11
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.....


Anuj Kumar<br />SCJP1.4<br />SCWCD1.4
Pranav Bhatt
Ranch Hand

Joined: Mar 20, 2006
Posts: 284
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

Joined: Feb 14, 2007
Posts: 63
Thanks a ton Anuj & Pranav.

You gave a very detailed explaination.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting Doubt