File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can't understand examlab downcasting question.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "can Watch "can New topic
Author

can't understand examlab downcasting question..

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

source : examlab practice exam 1



this code works fine...but according to me it should give runtime error at commented line but it doesn't. Please clear my doubt


OCPJP(83%)
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
The only reason I can think of is because C is apart of the same inheritance tree as B and B implements m


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Richard Broersma
Ranch Hand

Joined: Apr 28, 2009
Posts: 63

ankur trapasiya wrote:source : examlab practice exam 1this code works fine...but according to me it should give runtime error at commented line but it doesn't. Please clear my doubt
The best guess that I can give is that the value of Null is not type specific. Any type can be "pointed to" Null.
Prashant Shiralkar
Greenhorn

Joined: Apr 13, 2010
Posts: 19
The only reason I can think of is because C is apart of the same inheritance tree as B and B implements m

is a reason why the compiler doesn't complain when the downcast is made.

Whereas the reason for not having a run-time error is that the "Run Time Type Identification (RTTI)" or runtime type checking succeeds (It doesn't harm to cast null to C). Even though it looks like you are just performing an ordinary parenthesized cast [b = (C)m], at run time this cast is checked to ensure that it is in fact the type you think it is. If it weren't, you'd have got a ClassCastException. Try using b = (C)a and you will get a runtime error.

So, JVM only checks if the runtime object has the same type or is a subtype of the cast. This act of checking types at runtime is called RTTI.
Sunny Bhandari
Ranch Hand

Joined: Dec 06, 2010
Posts: 448

yes Prashant is correct.

Please note that null in "Min m = null" has nothing to do with no run time error.


Java Experience
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

still i am not getting this...... It should give me runtime exception because Min is not knowing any of the details about B or C. Please clear this doubt
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

1) You can cast a reference variable of type Interface to any non-final class even if they are not related. Read this tutorial for more information.
2) You can cast a null value to any type, there is never any ClassCastException in that...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can't understand examlab downcasting question..