The question might look trivial but I couldn't find the solution anywhere!
I have an abstract superclass Vehicle
, and there are 4 subclasses that extend it, viz Car
, Bus
, Lorry
and Bicycle
.
I also have an ArrayList<Vehicle> V
containing many Vehicle
objects but we don't know which type of vehicle.
Now I am not able to calculate the number of Cars
in the ArrayList V
int carCount = 0;
for (Vehicle ob: V){
if (ob is a car) {
carCount++;
}
}
return carCount;
Would like to know what should be the code inside if(ob is a car)
in the code above.
Thanks!