• 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

Casting Doubt

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a ton Anuj & Pranav.

You gave a very detailed explaination.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic