• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

casting problem

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Switching from electric heat to a rocket mass heater reduces your carbon footprint as much as parking 7 cars. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic