Is it possible to create
ArrayList;
I mean add obje
Get use of polymorphism. Let's say you have a parent class Vehicle for Bus and Car.
ArrayList list = new ArrayList();
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();
}