9

Is it possible to create ArrayList<Object type car,Object type bus> list = new ArrayList<Object type car,Object type bus>();

I mean add objects from different classes to one arraylist?

Thanks.

5 Answers 5

18

Yes, it's possible:

public interface IVehicle { /* declare all common methods here */ }
public class Car implements IVehicle { /* ... */ }
public class Bus implements IVehicle { /* ... */ }

List<IVehicle> vehicles = new ArrayList<IVehicle>();

The vehicles list will accept any object that implements IVehicle.

Sign up to request clarification or add additional context in comments.

Comments

9

Yes you can. But you need a common class to your object types. In your case this would be Vehicle.

So for instance:

Vehicle class:

public abstract class Vehicle {
    protected String name;
}

Bus class:

public class Bus extends Vehicle {
    public Bus(String name) {
        this.name=name;
    }
}

Car class:

public class Car extends Vehicle {
    public Car(String name) {
        this.name=name;
    }
}

Main class:

public class Main {
    public static void main(String[] args) {
        Car car = new Car("BMW");
        Bus bus = new Bus("MAN");
        ArrayList<Vehicle> list = new ArrayList<Vehicle>();
        list.add(car);
        list.add(bus);
   }
}

9 Comments

ok, what about when i read the lines from the file i don't know what object i read. I have the array of strings and i need to assign it to a correct object based on the attributes from file.?
This can not be directly solved with this implementation. You can have a function in Vehicle class called createVehicle, which will try to parse the input string, and create an appropriate object. For instance, the function could look-up which is the manufacturer of the vehicle - if it is BMW, then this is a Car, in other case it is a Bus.
Is this correct? the second array value is the objectType so i do this. 'ArrayList<Car> items = new ArrayList<Car>(); while ((strLine = br.readLine()) != null) { String s[] = strLine.split("\\|"); if(s[2].equals("Audi")){ Audi myAud = new Audi(s[0], s[1], s[2], s[4]); items.add(myAud); } else if(s[2].equals("BMW")){ BMW myBmw = new BMW(s[0], s[1], s[2], s[3], s[4]); items.add(myBmw); } else if(s[2].equals("Opel")){ Opel myOpe = new Opel(s[0], s[1], s[2], s[3], s[4]); items.add(myOpe); } else if(s[2].equals("Mazda")){ Mazda myMaz = new Mazda(s[0], s[1], s[2], s[3]); items.add(myMaz); } }'
Yeah, something in those lines. But here you need to sub-class all car manufacturers, which would take a lot of time, and also it is not modular. So I would stick with the representation, where you have class Car. And then while constructing an object, you would call new Car(s[0],s[1],s[2],s[3],s[4]), where s[2] is obviously the car brand.
For that, see stackoverflow.com/questions/2784514/… . You need to implement custom Comparator.
|
7

Get use of polymorphism. Let's say you have a parent class Vehicle for Bus and Car.

ArrayList<Vehicle> list = new ArrayList<Vehicle>();

You can add objects of types Bus, Car or Vehicle to this list since Bus IS-A Vehicle, Car IS-A Vehicle and Vehicle IS-A Vehicle.

Retrieving an object from the list and operating based on its type:

Object obj = list.get(3);

if(obj instanceof Bus)
{
   Bus bus = (Bus) obj;
   bus.busMethod();
}
else if(obj instanceof Car)
{
   Car car = (Car) obj;
   car.carMethod();
}
else
{
   Vehicle vehicle = (Vehicle) obj;
   vehicle.vehicleMethod();
}

2 Comments

that is exactly what am i doing now but how can i acces the methods from extended classes?
Is this correct? the second array value is the objectType so i do this. 'ArrayList<Car> items = new ArrayList<Car>(); while ((strLine = br.readLine()) != null) { String s[] = strLine.split("\\|"); if(s[2].equals("Audi")){ Audi myAud = new Audi(s[0], s[1], s[2], s[4]); items.add(myAud); } else if(s[2].equals("BMW")){ BMW myBmw = new BMW(s[0], s[1], s[2], s[3], s[4]); items.add(myBmw); } else if(s[2].equals("Opel")){ Opel myOpe = new Opel(s[0], s[1], s[2], s[3], s[4]); items.add(myOpe); } else if(s[2].equals("Mazda")){ Mazda myMaz = new Mazda(s[0], s[1], s[2], s[3]); items.add(myMaz); } }'
2

You can't specify more than one type parameter unfortunately, so you'll have to find a common superclass for your types and use that. An extreme case would be just using Object:

List<Object> list = new ArrayList<Object>();

Be careful that you will need to cast the result to the specific type that you need if you retrieve an item (to get full functionality, not just the common one):

Car c = (Car)list.get(0); 

1 Comment

Thanks, but in my case i cannot use this solution because i read the attributes from the array and i don't know what object i get from file.
0

Create a class and use polymorphism. And then to pick up the object in the click, use instanceof.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.