wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "casting problem" Watch "casting problem" New topic
Author

casting problem

payal sharma
Ranch Hand

Joined: Oct 04, 2000
Posts: 57

C:\JAVA>java Test
true
java.lang.ClassCastException: Base
java.lang.ClassCastException: Base
true
Here I am unable to understand the output the class cast exception with
Base two times and then true why?
Please explain this.
thanks in advance
payal
[This message has been edited by payal sharma (edited July 27, 2001).]
Mikael Jonasson
Ranch Hand

Joined: May 16, 2001
Posts: 158
The two exceptions for base are:
Cloneable c1=(Cloneable)x;
Cloneable c2=(Cloneable)x;
When called with base. Since all objects extends Object, Test will always print "true" when called with an object (both base and child are objects). Since base implements runnable and child extends base, the cast to runnable will work for both. Child implenets colneable, so the above cast will work for it.
Dow this explain it?
/Mike
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Payal,
The Base class doesnot implement Cloneable.Hence you cannot cast its instance to Cloneable.You get the two exceptions for statements :

Cloneable c1=(Cloneable)x;
Cloneable c2=(Cloneable)x;

when x is an instance of Base class.
When x is a instance of Child class, the above statements would not give ClassCastException , since Child class implements Cloneable.
When either of the constructors are invoked, each being an instanceof the Object prints true in the output.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)

<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting problem
 
Similar Threads
please explain me why this program is not going into deadlock scenario
cloning doubt
synchronized method
Deep Copies?
classes