Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assignment question doubt ??

 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • 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
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Ramesh Mangam
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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?
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • 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..

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic