• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reference Casting

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class X {
public static void main(String args[] ) {
A[] arrA;
B[] arrB;
arrA =new A[10];
arrB = new B[20];
arrA=arrB; //1
arrB=(B[])arrA; //2
arrA= new A[10];
arrB =(B[]) arrA; //3
}
}
class A{}
class B extends A {}
the options are:
1.the program will throw a java.lang.ClassCastException at line labeled 2 when run
2.the program will throw a java.lang.ClassCastException at line labeled 3 when run.

[This message has been edited by charu (edited March 08, 2001).]
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi charu,
You have not defined the type of variable arr in line 3. I expect you meant it to be arrB. In that case, line 3 will give a ClassCastException. That is because you are assigning a superclass array object to a subclass field.
For example, if A is the superclass and B is the subclass, you can write
A a = new B();
but you can't write, B b = new A();
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charu!
There is no way of casting userdefined classes unless until they are one of them is derived from the other.
You need to write special methods for that.
Excerpts from Thinking in Java


Java allows you to cast any primitive type to any other primitive type, except for boolean, which doesn�t allow any casting at all. Class types do not allow casting. To convert one to the other there must be special methods. (String is a special case, and you�ll find out later in this book that objects can be cast within a family of types; an Oak can be cast to a Tree and vice-versa, but not to a foreign type such as a Rock.)



Siva
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Charu,
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
Thanks for your cooperation.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The option 2 is correct. It throws ClassCastExsception in line labeled 3. The reason is you cannot cast a parent to child. In line labeled 2 arrA was pointing to array of objects of class B.
-- Hari Gangadharan
 
charu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
arrB is a subclass of arrA,so why shold it not throw a ClassCastException in line 2 itself.
What is the difference betwen lines 2 and 3.
please clear this difference.

thanks
charu.

Originally posted by niraj singh:
Hi charu,
You have not defined the type of variable arr in line 3. I expect you meant it to be arrB. In that case, line 3 will give a ClassCastException. That is because you are assigning a superclass array object to a subclass field.
For example, if A is the superclass and B is the subclass, you can write
A a = new B();
but you can't write, B b = new A();


 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class X {
public static void main(String args[] ) {
A[] arrA;
B[] arrB;
arrA =new A[10];
arrB = new B[20];
arrA=arrB; //1
arrB=(B[])arrA; //2
arrA= new A[10];
arrB =(B[]) arrA; //3
}
}
Hi !!
it wont show class cast exception at 2 bcoz,at 1 arrA is pointing towards B.arra has now the address of B.So it wont throw any exception.
Before 3,u r creating a new object of A.it doesn't contain any address of B.
There is one simple rule for class cast exception,which i have discoverd and it works fine.
(Cast)X,wont throw any exception if x is instance of Cast.If it is not then it will.
Now at one arra points towards B.So arrA is instanceof B.At three ,this is not the case.And arrA instanceof B returns false,hence classcast exception.
hope this will clarify your doubt.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic