This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Casting  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Casting  " Watch "Question on Casting  " New topic
Author

Question on Casting

aishwarya mathur
Greenhorn

Joined: Sep 03, 2004
Posts: 22
Can anyone explain me the below code from Khalid's on pgno.268
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
What do you want to know about it?


Mark Patrick<br />SCJP 1.4
Ozgur Nickpherly
Greenhorn

Joined: Sep 23, 2004
Posts: 3
I think question:
What is the result of code?
arrays are object so they can be cast to super class arrays. But casting to subclass occurs Runtime Exception.

public class Myclass {
public static void main(String args[]) {
A[] arrA;
B[] arrB;
arrA = new A[10];
arrB = new B[10];
arrA = arrB;
arrB = (B[])arrA;
arrA = new A[10]; //arrA has A object array
arrB = (B[]) arrA; //Run time error occurs while casting to subclass
}}
class A{}
class B extends A{}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Casting
 
Similar Threads
Arrays
casting doubt
Need help in casting topic
Reference Casting
Ques on casting referances ...