This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assignment question doubt ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assignment question doubt ??" Watch "assignment question doubt ??" New topic
Author

assignment question doubt ??

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252


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


Thanks & Regards<br /> <br />-Srikanth
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
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 ]

Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
srini iam getting
incompatible types; found: Base[], required: Object
cannot cast Object to Sub[]
cannot cast Object to I1[]
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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

Joined: May 03, 2005
Posts: 13
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

Joined: Aug 18, 2005
Posts: 220
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.


"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
Ramesh Mangam
Greenhorn

Joined: May 03, 2005
Posts: 13

[ October 20, 2005: Message edited by: Ramesh Mangam ]
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
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..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assignment question doubt ??