Help with accessing objects in ArrayList
Hello everyone,
I would like to know why the line "System.out.println(cars.get(0).getType());" is failing to compile? This is a test class that I wrote up, because my main application is having the same issue.
Here is the code:
import java.io.*;
import java.util.*;
public class Test
{
public static void main(String[] args)
{
ArrayList<Car> cars = new ArrayList<Car>();
cars.add(new Ford("Musting",10000));
System.out.println(cars.get(0).getType());
}
}
class Car
{
int price;
Car(int price)
{
this.price = price;
}
}
class Ford extends Car
{
String type;
public Ford(String type, int price)
{
super(price);
this.type = type;
}
public String getType()
{
return type;
}
}
[901 byte] By [
dracicota] at [2007-11-27 7:57:16]

Next time paste in the exact, complete error message, and indicate which line it's talking about.The Car class doesn't have a getType method. Your Test class does, but it returns a type variable that's not present in the Test class, so that won't compile either.
jverda at 2007-7-12 19:39:06 >

Thanks jverd,The error message is:C:\Dev\Java\Test.java:20: cannot find symbolsymbol : method getType()location: class CarSystem.out.println(cars.get(0).getType()); ^1 error
Why does my Car class have to have a "getType" method? Just because I am extending Car class from Ford class, I didn't think they needed the same methods. How else can I extract a Ford object from my ArrayList, and still access the methods in Ford class?
The getType method is in the Ford class not the Car class and you ArrayList holds Car objects not Ford objects. You will neeed to cast the object returned from the get method to a Ford object before calling the getType method.
> Why does my Car class have to have a "getType"
> method?
Because you're trying to call getType on a Car object.
> Just because I am extending Car class from
> Ford class, I didn't think they needed the same
> methods.
If Ford extends Car, then Ford inherits all of Car's non-private methods. But neither Car nor Ford has getType. Only Test does.
jverda at 2007-7-12 19:39:06 >

Actually jeff the getType method is in Ford class.
Thank you for the responses jverd, and flounder.
I tried casting to a Ford object, and still the same error.
public class Test
{
public static void main(String[] args)
{
ArrayList<Car> cars = new ArrayList<Car>();
cars.add(new Ford("Musting",10000));
System.out.println((Ford)cars.get(0).getType());
}
}
--
C:\Dev\Java\Test.java:19: cannot find symbol
symbol : method getType()
location: class Car
System.out.println((Ford)cars.get(0).getType());
^
1 error
That's because you are casting what is returned from getType to a Ford object not what is returned from the get method.System.out.println( ((Ford)cars.get(0)).getType()); // notice extra brackets
That did it flounder - Thanks.Also, is this the best approach for what I am trying to accomplish? Or is there a much cleaner way of doing this?All help appreciated.
One problem you will have is if the get method returns a Car object.
> Also, is this the best approach for what I am trying
> to accomplish? Or is there a much cleaner way of
> doing this?
This looks to me to be a very dangerous way of doing things. As I see things, you have no guarantee that an object in the cars ArrayList will be a Ford. If you try to cast a non-Ford object as a Ford, the system will come crashing down. If Car is abstract and all classes subclassed from Car will have a getType method, then call Car abstract and put in an abstract getType method in it. Then no cast will be needed and you will allow polymorphism to do its magic if and when needed.
> Actually jeff the getType method is in Ford class.Oops. So it is. My bad.I guess I miscounted braces. I thought Car and Ford were nested inside Test, and getType was a method of Test.Sorry for the confusion.
jverda at 2007-7-12 19:39:06 >
