Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassCastException

 
Mark Beavis
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All -

Can someone tell me why the last line of code in the main() method throws a ClassCastException - I cannot figure it out...



Thanks in advance...
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Mark Beavis
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then wouldn't the previous line cause an exception also:



This seems to compile fine...

I actually thought that both lines would compile, since B[] IS-AN A[]
 
Surekha Reddy
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If u comment the line a=b then b = (B[])a will also give ClassCastException.

After a=b statement a is also referring to B[] object. So it won't give any exception.

Correct me if i'm wrong.

Regards,
Surekha.
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mark,

I don't really think that B[] IS-AN A[]. Of course, I am sure others would correct me if I am wrong (I am trying to take the certification too).

But the reason is, B[] and A[] are of type Array Objects, whose elements would contain A and B objects/references. So you could say something like

a[0] = (A) b[0]; //I am not sure if the casting is correct yet, but you get the picture right?

AND NOT

b = (B[]) a;

because, as per the code, B extends A

AND NOT

B[] extends A[].

Please correct me if I am wrong.

Thanks,
-Vijay
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then wouldn't the previous line cause an exception also:

No, because the 'a' reference points to the B[20] array at that point.
 
Vijay Gade
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just found out that my post was wrong. Please excuse me if you had alread read it in the first place.
Thanks,
-Vijay
 
Patrick Williams
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

then wouldn't the previous line cause an exception also:





Not when line 6 says

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.public class ClassCastTest {
2.public static void main(String args[]){
3.A[] a,a1;
4.B[] b;
5.a = new A[10];
6.a1 = a;
7.b = new B[20];
8.a = b;
9.b = (B[])a;
10.b = (B[])a1;
11.}
12. }
class A{}
class B extends A{}

hi mark, here�s the explanation

Line 10 gives a class cast exception. Now lets see how and why-

The theme of the question is �object reference concersion and casting�, While answering such questions keep in mind a basic thumb rule �

NewType=OldType ; //Thumb rule- for conversion to be legal, �OldType� should be a subclass of
//NewType

Line 6 is fine because both a1 and a are references of the same class.
Line 8 is fine because b (an array reference variable) is reference of class B which is a subclass of class A
So this is fine as per the above thumb rule

Lines 9 and 10 have explicit cast used. Another thing that one should remember is that whenever one sees an explicit cast, one must do the following two checks in mind(in fact the compiler checks like this)-

1)Comile time check- check whether the reference on either side (right/left) is a subclass of that on
another side. At lines 9 and 10, above this condition is satisifed get any compiler error Since you
are casting a reference of a superclass into a reference of a subclass, the explicit cast is
necessary
2)Run time check � here you need to determine the runtime classes of references on both sides
For line 9, class of �a� is B (due to line 8). So line 8 is just casting a reference of class B to a refernce �b� ( which is again an objec reference of class B). This in O.K
But at line 10, run time class of �a1� is class A, whereas runtime class of �b� is B. Thus irrespective of the cast, assignment on line 10 is something like this �
Baseclass=superclass,
i.e. the purpose of using a cast has gone in vain.
Hence we get a runtime exception

Hope you got it. CORRECT ME IF I AM WRONG ANYWHERE.
 
Krishna Latha Grandhi
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Surekha Reddy:
Hi,

If u comment the line a=b then b = (B[])a will also give ClassCastException.

After a=b statement a is also referring to B[] object. So it won't give any exception.

Correct me if i'm wrong.

Regards,
Surekha.


Hi Surekha,

you are right..

krishna.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic