Let's say that I have abstract class Car and there are x concrete classes (RedCar, BlueCar ...) that extend class Car. Then I have class Garage that has property Car[] cars.

I want to write a boolean method to class Garage that will loop through the cars and returns true if searched car (one of concrete type (RedCar, BlueCar..) passed as parameter) was found, otherwise returns false.

What is the best way to do it?

For now, I have something like this :

```
public boolean hasCar(Class<? extends Car> c) {
    for (int i = 0; i < this.cars.length; i++) {
        if (c.isInstance(this.cars[i])) {
            return true;
        }
    }
    return false;
}

```

but wouldn't it be better if I create an enum with all possible Car types (subclasses of Car), add a property to class Car, which will hold the constant from enum and compare it based on it?

Something like this:

```
public enum CarType{
    RED_CAR, BLUE_CAR
}

public abstract class Car{
    public CarType type;
    Car(CarType type){
        this.type = type;
    }
}

public class RedCar extends Car{
    public RedCar(){
        super(CarType.RED_CAR);
    }
}

public class BlueCar extends Car{
    public BlueCar(){
        super(CarType.BLUE_CAR);
    }
}

public class Garage{
    private Car[] cars;
    public boolean hasCar(CarType type) {
        for (int i = 0; i < this.cars.length; i++) {
            if(this.cars[i].type == type){
                return true;
            }
        }
        return false;
    }
}

```

Which one is the better method?