• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

assignment question doubt ??

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


can anyone explain the answer for this i.e f....i ran it it was giving compilation error also....i thought everything to be correct with this question..
please clarify.....

thanks
sri
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes this code should give a runtime error at line 3 because .. A super class cannot be casted to a sub class as it doesnt know anything about it's subclasses. It should give a ClassCastException at runtime.

It's something like this all 'Strings' are Object but vice versa is not true.

What error are you getting at compile time?
[ October 19, 2005: Message edited by: Srinivasa Raghavan ]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
srini iam getting
incompatible types; found: Base[], required: Object
cannot cast Object to Sub[]
cannot cast Object to I1[]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
srini




in the first code iam not getting any class cast exception..and the second one gives me the cast exception....can anyone please explain me the logic behind???
thanks
sri
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srikanth,



In the first code above, the flow goes as follows: s1 is pointing to a Sub object, s1 reference is assigned to Base reference base at line 3 which means that base reference is also pointing to the same Sub object as s1. So, line 4 is legal as we can cast a superclass reference to subclass if the superclass reference is actually poiting to a subclass object.

Now, watch the secod code:



Here base refers to Base object. So, when you cast a superclass reference to a subclass reference you will get a runtime error. Hope this helps. But, I am still not sure how there is compile error in the first example. I compiled it and did not get any error. Are you sure there is no typo?
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats what he's saying, there's no error in the first one while there is in the second.
but you've given the right exlpanation anyway.
 
Ramesh Mangam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ October 20, 2005: Message edited by: Ramesh Mangam ]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ramesh ...thats for ur reply...those two examples i got it ..
but the first one still gives me the same compiler error ...
guys cud u please try it out and explain me .....

thanks
sri
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ramesh Mangam:
Sorry. I screwed up with the formatting...


Note that you can edit your posts by clicking on the paper/pencil icon.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but the first one still gives me the same compiler error ...



The first piece of code compiles fine ...and we get runtime error...in

line 3...the reason...ClassCastException...

Can you reproduce the compilation error..so that we can help you out..

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic