File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Casting  " Watch "Question on Casting  " New topic

Question on Casting

aishwarya mathur

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

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:
subject: Question on Casting
It's not a secret anymore!