Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

arraylist problems

Status
Not open for further replies.

Krolsky

Programmer
Apr 16, 2002
20
DE
hi there
i got a program using corba. however this is a problem i got when pulling objects out of an arraylist i create.

i create the arraylist and i put (to test) 1 object in it. as far as i know the object i put in it is type "Object" after i did list.add(myObject).

now i have a method to get the object out of the arraylist again that looks like this:

public Car getCar(ID){}

due to the fact that i'm working with corba i can not instantiate the Car object because it's an abstract class. now when i do list.get(listspot) i get an "Object" object. but to be able to return it i have to put that into a Car object. if i dont do this the compiler will not work :)
i do have an implementation class of Car called CarImp.
i tried to do something like this:

public Car getCar(listspot){
CarImp theCar = (CarImp) list.get(listspot);
return theCar;
}

this will not work because here a "CarImp" object is returned instead of a "Car" object.

the question here is if there's any way to turn the CarImp object into a Car object or make the compiler think so?
 
While you cannot do Car car = new Car() because the Car class is abstract, you can cast an object to a Car. Try:

public Car getCar(listspot){
Car theCar = (Car) list.get(listspot);
return theCar;
}

-gc
"I don't look busy because I did it right the first time."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top