aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes casting 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 "casting" Watch "casting" New topic
Author

casting

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383


Z & Y are sibling objects here. Line 1 fails at runtime but why it compiles?


SCJP-1.5<br />SCWCD-1.4
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Z y2 = (Z)y; //line 1

'y' is a variable of type 'X' that refers to an object of type 'Y' and you are casting it to type 'Z' which is a sub class of 'X' so it pass at compile time. Had it been of type 'Y' Not 'X' then it wouldnt compile.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks Ahmed I got it. I was little confuse.
yogesh sood
Ranch Hand

Joined: Aug 31, 2000
Posts: 108
Dolly,

You should have look at this from Corey, to understand how it works for Interfaces.



If its green its biology if its stinkks its chemistry if it has numbers it is Maths and if it doesn't work its TECHNOLOGY
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
You can not convert a car on a bike, only because both are vehicles. See the problem that prevents the compiler?
I hope I have helped!


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting