2

I have Car object which implements Runnable as below

Car implements Runnable{
 int id; 
 public Car(int id) {
  id = this.id;
  }
  public boolean getIsAvailable() {
   return isAvailable;
  }
}

class CabService{
 static List<Thread> cars = new ArrayList<Thread>();
 public static void main(String[] args) {
  for(int i=0; i< 3; i++)
    {   
        cars.add(new Thread(new Car(i)));
    }
  }
}

In CarService class I have list of cars as below and trying to iterate over list and cast the object to Car as below

    List<Car> availableCars = new ArrayList<Car>();
    List<Thread> cars = CabService.cars;    
    for(Thread carThread : cars) {
        Car car  = (Car)carThread;
        if(car.getIsAvailable())
            availableCars.add(car);
    }
    return availableCars;

When I try to cast the carThread to car I get compilation error Cannot cast from Thread to Car Can someone help me how to fix this?

dotnetom
  • 24,551
  • 9
  • 51
  • 54
OTUser
  • 3,788
  • 19
  • 69
  • 127
  • 3
    Thread is a class and not an interface. So you extend Thread not implements – Peshal Nov 05 '14 at 04:28
  • 1
    And `getIsAvailable()` is not `run()`. – Elliott Frisch Nov 05 '14 at 04:32
  • 1
    `Runnable` is not the same as `Thread`. – user253751 Nov 05 '14 at 04:32
  • 1
    You still need to fix your question, and it suggests that you're still not clear on the difference between a Runnable, a Thread, and a Car. The compiler is right -- the cast that you're trying to do is illegal and shouldn't be done. – Hovercraft Full Of Eels Nov 05 '14 at 04:34
  • Alternately, you can implement the Runnable interface. It's not how you're using threads in this situation, so which (Runnable, or extending Thread as Peshal mentions) is preferable is unknown. Also you should avoid doing the cast '(Car)carThread'. There a number of ways to avoid this, but most come down to why you know all elements in cars are of type Car. – D'Nabre Nov 05 '14 at 04:35
  • If you're implementing Runnable than, you can't cast to Thread, you have use Runnables, and cast to that. As said, blind casts like that just aren't good, and suggest some assumption about types you should make manifest in your code. – D'Nabre Nov 05 '14 at 04:37
  • http://stackoverflow.com/questions/541487/implements-runnable-vs-extends-thread – Sotirios Delimanolis Nov 05 '14 at 04:43
  • @RanPaul your question is having error in itself. – Kishan Bheemajiyani Nov 05 '14 at 05:35

1 Answers1

2

Your solution is to change CabService.cars to be a List<Car> not a List<Thread>. This way, there's no need to attempt a wrong cast, and in fact no cast would be needed. I'd also bet that you'd be better off to make this variable not a static field but rather make it a private instance field and access it with a getter method. Why is this important? Well one reason is that this allows you to later make the List to be read only, to not allow other classes to change the Cars in the list, by having the getter pass a deep copy List.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373