aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "casting doubt" Watch "casting doubt" New topic
Author

casting doubt

pritam kamkar
Greenhorn

Joined: Jul 23, 2008
Posts: 3
hey people i got this question on the mock exam


the answer says that the cast on line1 will fail at runtime can anyone please tell me why does it work during compile time
Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45



At compile type, compiler doesnot know about the real object at line1,thing it only knows about is that y is a referance of class z. so casting z into z will have no problem.

Now at run time real object is of class y and there is no inheritance between z and y.Ttherefore it will give an error.

Regards
Sunny


Regards
Sunny Mattas
SCJP5
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Pritam,

Z y = new Y();
will not compile, Z & Y don't belong in the same class hierarchy.

Where is this question from ?
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by pritam kamkar:
hey people i got this question on the mock exam


the answer says that the cast on line1 will fail at runtime can anyone please tell me why does it work during compile time


Hello, this is a question from Bonus Master Exam i guess, and you might have copied wrong, because the line that gives an error in your exmaple is the line above line1.

I think the original is:


and it fails on line 1 because y is of the type Y and it fails on the is a relationship, Y can't be casted to a type Z, it will fail in the test y instanceof Z.

I hope it helps.

Kind Regards,
Raphael Rabadan


SCJP Java 6 (98%) - Story, SCJA (88%) - Story
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 318
I think a simple way to think of it is like this: a cast will fool a compiler into putting anything into a reference without squawking about it. But, at run time, the JVM knows what kind of object it REALLY is and will give you the old ClassCastException when it isn't a match.


------------------------
Bob
SCJP - 86% - June 11, 2009
Sazzad Hossain
Greenhorn

Joined: Jul 21, 2008
Posts: 10
To Raphael Rabadan
Does it mean casting is not possible if there is no is-a relationship in two classes?
Raphael Rabadan
Ranch Hand

Joined: Jul 05, 2008
Posts: 141
Originally posted by Sazzad Hossain:
To Raphael Rabadan
Does it mean casting is not possible if there is no is-a relationship in two classes?


Yes. You can't use instanceof too.
See this code...


1st Case A newA = (A) bAsB; - Here we can't cast because the reference type of bAsB is B and newA is A and A won't pass in the relationship test A is an B

2nd Case A anotherA = (A) bAsC; - Here we can cast because the reference type of bAsC is C and anotherA isA A and A pass in the relationship test A is an C. You'll get ClassCastException on runtime here. Because, at runtime the compile will see that C was an B, and A isn't an B, so, can't be casted.



Try this other code too..



Kind Regards,
Raphael Rabadan
[ July 24, 2008: Message edited by: Raphael Rabadan ]
Sazzad Hossain
Greenhorn

Joined: Jul 21, 2008
Posts: 10
Make sense. Thanks a lot for clarification
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: casting doubt
 
Similar Threads
k&b master exam :polymorphism doubt
constructor
casting
Error in K&B second MasterTest , Q 49 ?
Object casting