Casting To The Correct Subclass
<p>I hava a supeclass called Car with 3 subclasses.</p>

<pre><code>class Ford extends Car{

}

class Chevrolet extends Car{

}


class Audi extends Car{

}
</code></pre>

<p>Now i have a function called <code>printMessge(Car car)</code> which will print a message of a particular car type. In the implementation i  use if statements to test the instance of the classes like this.</p>

<pre><code>public int printMessge(Car car){
     if((Ford)car instanceof Ford){
            // print ford

     }else if((Chevrolet)car instanceof Chevrolet){
            // print chevrolet

     }else if((Audi)car instanceof Audi){
            // print Audi
     }
}
</code></pre>

<p>for instance if i call it for the first time with Ford <code>printMessge(new Ford())</code>, it prints the ford message but when i call it with <code>printMessge(new Chevrolet())</code>, i get <strong>EXCEPTION</strong>  from the first if statement that Chevrolet cannot be cast to Ford.</p>

<p>What am i doing wrong and what is the best way.</p>

<p>thanks</p>
