Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on Casting

 
aishwarya mathur
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain me the below code from Khalid's on pgno.268
 
Mark Patrick
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you want to know about it?
 
Ozgur Nickpherly
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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{}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic