• Post Reply Bookmark Topic Watch Topic
  • New Topic

A small doubt in Downcasting  RSS feed

 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
Please see the code below:



The OUTPUT of above code is:

The b.getSize() returns=100
Hi inside hahaHaa() method
Hi this.getSize()=210
Hi this.getB1()=21
Exception in thread "main" java.lang.ClassCastException: A
at Inheritance1.main(Inheritance1.java:49)

My Question: Have I not used a proper downcast?
b=(B)a; // Problem starts from here
Even if I comment this statement: b.hahaHaa(); (see line of code) then also the java.lang.ClassCastException comes up.

Please explain clearly that what is the reason of this runtime exception?

Thanks & Regards,
Nitin Bhardwaj


[HENRY: Added Code tags, and formatted code]
[ June 09, 2007: Message edited by: Henry Wong ]
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Explicitly casting an object in Java does not convert the object -- as it may do with primitives. It merely tells the compiler that you know what you are doing and allow it.

In your case, you cast an object that is clearly an A object into being a B object, which it is not. The compiler may allow it, but at runtime, it will not.

Henry
 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry !! But please tell me one thing that if this is true then what is the utility of Downcasting when it comes to Objects(or Reference Varaibles)? OR Is Downcasting of Objects really possible or not in any case? If yes then can you please provide an example if it is not a problem for you?
Thanks so much !!
Nitin
 
Henry Wong
author
Sheriff
Posts: 23280
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nitin Bhardwaj:
Thanks Henry !! But please tell me one thing that if this is true then what is the utility of Downcasting when it comes to Objects(or Reference Varaibles)? OR Is Downcasting of Objects really possible or not in any case? If yes then can you please provide an example if it is not a problem for you?


You cast an object to another type only if you know if it is that type. This example is too short to be a useful, but for example, if this was a method.



In this case, the object that is referred to by the "a" reference may indeed be a B object type. If you can confirm it, or if you know it, you can cast and it should work.

With this method, you can use it like this...



It will cast in the first method call, but not in the second call.

Henry
[ June 09, 2007: Message edited by: Henry Wong ]
 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry I think that I understood.
Let me check with you through following code:

class A{}
class B extends A{}
public class Inheritance2
{
public static void main(String args[])
{
A a1=new A();
A a2=new A();
B b1= new B();
B b2= new B();
a1=b1;
b2=(B)a1; // This is OK coz a1 actually refers to an object of class B
System.out.println("The type of an Object is considered at runtime while performing a downcast");
b2=(B)a2; // But this will cause ClassCastException coz a2 refers to an object of class A not B
System.out.println("You will not see this line until the line above is commented");
}
}


I deduced from above discussion that the type of an object is considered by the JVM while performing a downcast. If the actual type of an object(being referred by a superclass reference variable) is exactly same as the type of target reference variable(to which it is being downcasted) then the runtime exception java.lang.ClassCastException is not thrown but if there is a mismatch between the types then java.lang.ClassCastException is thrown.
Is this right Sir?
Regards,
Nitin
 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitin,
You are right.





Regards
NIK
SCJP 1.5
 
Nitin Bhardwaj
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nik for conforming !!
Regards,
Nitin
 
Arnav Velimala
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case, you cast an object that is clearly an A object into being a B object, which it is not. The compiler may allow it, but at runtime, it will not.


Just an FYI,
the compiler allows because it just checks whether there is an IS-A relationship between the two reference types.
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, what you were trying to do is a *downcast*, as you said. As with the examples what Henry showed, he is checking the B instance of A and then only he proceeds. Otherwise also it may thrown a runtime classCastException because of,

A superclass reference can hold the subclass reference/objects whereas the reverse is not true!

In your example(s), the object of type "A" can hold the objects of type "B" as well since the class B extends A. But the reverse is not true - means, the object of type "B" cannot hold the objects of type "A" unless its explicitly downcasted.

As you had very well downcasted, but its okay until the compile-time as you told the compiler that you are aware of the consequences. But during the runtime it blows up.

In one of your examples (last one), the reference variable "a1" being assigned to the reference variable "b1" through the explicit downcast "(B)" is allowed and there is no runtime error, because the reference variable "a" of type "A" is actually pointing to the object of type "B" in the previous statement.

HtH and concludes.
[ June 11, 2007: Message edited by: Raghavan Muthu ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!