Suppose I have this
interface Movable
{//some stuff}
And I have
class Car implements Movable
{//some stuff}
And also maybe I have
class Bike implements Movable
{//some stuff}
I noticed that if I had this:
ArrayList<Movable> movableThings = new ArrayList<Movable>();
movableThings.add(some kind of Car)
movableThings.add(some kind of Bike)
movableThings.add(some kind of Bike)
This can be called:
for(Movable m: movableThings)
But if I call this I get incompatible types:
for(Bike b: movableThings)
Can someone explain, and maybe offer a better way? I know I can use foreach Movable m: movableThings and then use instanceof to check for Bikes but is there another way?
Edit: alright thanks for clarifying guys... so I guess I either use instanceof or redesign my game