This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ClassCastException going from interface to object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "ClassCastException going from interface to object" Watch "ClassCastException going from interface to object" New topic
Author

ClassCastException going from interface to object

Alan Smithee
Greenhorn

Joined: Mar 21, 2006
Posts: 23
Given the class
===================
public class AllStuff implements Serializable {

public List <IBigCar> bigcars;

}


And the following code
=======================

BigCar bc = new BigCar();
bc = (BigCar) this.classname.getAllStuff("uniqueID").getBigCarList().get(0);


This code compiles. Why am I not able to cast an interface from a list to an object?
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
If the runtime type is actually a BigCar then the cast should work. If you are getting a ClassCastException, then the run time type must not be a BigCar.



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: ClassCastException going from interface to object
 
Similar Threads
Threads,serialisation, static variables - java basic doubts
hashcode() and equals()
need some Help
Polymorphism?
Skipping element addition in list