• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting qs from Whizlabs

 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, following code is from Whizlabs



***************************************************************************

interface Inter{}

class Base implements Inter{}

class Derived extends Base{}

public class Test{

public static void main (String [] args){

Base b = new Base();
Derived d = new Derived();
Inter i = (Base)b;// LINE 1
i = (Base)d; // LINE 2
Derived bd = (Derived)b; //LINE 3
b = (Base)i; //LINE 4


}
}

***************************************************************************


I mapped out the following hierarchy:

IS-A Implements
Derived ------> Base -------------> Inter


So since all the classes are in the same hierachy, and all casts were marked explicitly then I believed the above code would compile correctly.

But the real answer is that LINE 3 causes a ClassCastException at runtime as a Base class objec is being cast to a derived class type.


Please can anyone explain why this is the case? I thought LINE 3 would run as it is explicity cast to a Derived type.


Thanks
 
Anuj Prashar
Ranch Hand
Posts: 99
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Santiago
The thing is that b is-a Base And we are converting it to Derived by casting,since b is of Base type and is not Derieved which is its child class so there is classcast exception.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Santiago Bravo:

So since all the classes are in the same hierachy, and all casts were marked explicitly then I believed the above code would compile correctly.

LINE 3 would run as it is explicitly cast to a Derived type.


The cast will result in no compilation error, but there will be a ClassCastException as you are trying to convert an object of super type to sub type. This is not allowed at run time. Try to make some codes and experiment with this...
 
Santiago Bravo
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the replies all!

So basically what you are saying is that if the classes are in the same hierarchy (IS-A or implements) then with explicit casts there should not be any compliler error. If I did someting like:

Inter i = (String)b;// LINE 1

This would give a compiler error.

Now in regards to LINE 3 the error is there because the object type is Base and its trying to cast it to a subtype. LINE 1 is ok as 'b' is of type Base, LINE 2 is ok as 'd' is being cast to a supertype and LINE 4 is ok as 'i' is already a Base object.


Is this correct?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right. Casting in same hierarchy doesn't gives a compilation error but if you cast an object (by object I mean actual object and not reference) of super-type to sub-type, there will be a ClassCastException...
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic