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.
See Question&Answers more detail:osIs 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.
See Question&Answers more detail:osYes, 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
.