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 qs from Whizlabs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting qs from Whizlabs" Watch "Casting qs from Whizlabs" New topic
Author

Casting qs from Whizlabs

Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
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


Santiago
My Path to SCJP Certification My Path to SCWCD Certification
Anuj Prashar
Ranch Hand

Joined: Apr 15, 2008
Posts: 99

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.


SCJP 5,SCWCD 5, RHCT
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Santiago Bravo
Ranch Hand

Joined: Jul 25, 2008
Posts: 226
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

Joined: Aug 03, 2008
Posts: 9321
    
  17

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...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Casting qs from Whizlabs