Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass
I'm trying to print out the details from the subclass auctionsale but I'm getting:
Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass the subclass extends the Superclass.
I've been calling a method from the subclass.
Am I on the right track or what should I be reading?
I'm using
Thanks in advance for your help.
Regards
Gerard
Code java:
((SubClass)thing[i]).printDetails();
Re: Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass
The compiler won't lie, so perhaps things are not subclassed the way you think they are.
Post a brief example of code that generates that message. It would be useful to see the relevant things (the classes and that array) being declared.
Re: Exception in thread "main" java.lang.ClassCastException: SuperClass cannot be cast to Subclass
For the sake of debugging your class hierarchy if it is really complex, try printing out the list of superclasses for the object in the array. To do that, add this bit of code before you cast it, and tell us what it prints out:
Code java:
Class C = thing[i].getClass();
while (C != null) {
System.out.println(C.getName());
C = C.getSuperclass();
}