Java downcasting dilemma
<p>please have a look at this code here.</p>

<pre><code>class Vehicle {
    public void printSound() {
        System.out.print("vehicle");
    }
}

class Car extends Vehicle {
    public void printSound() {
        System.out.print("car");
    }
}

class Bike extends Vehicle{ // also tried to extend Car
    public void printSound() {
        System.out.print("bike");
    }
}

public class Test {
    public static void main(String[] args) {
        Vehicle v = new Car();
        Bike b = (Bike)v;
        v.printSound();
        b.printSound();

        Object myObj = new String[]{"one", "two", "three"};
        for (String s : (String[])myObj) System.out.print(s + ".");


    }
}
</code></pre>

<p>Executing this code will give <code>ClassCastException</code> saying <code>inheritance.Car cannot be cast to inheritance.Bike</code>. </p>

<p>Now look at the line <code>Object myObj = new String[]{"one", "two", "three"};</code>. This line is same as <code>Vehicle v = new Car();</code> right? In both lines we are assigning sub class object to super class reference variable. But downcasting <code>String[]myObj</code> is allowed but <code>(Bike)v</code> is not. As mentioned in the comment I also tried to extend Car using bike. According to some discussion here, Bike is not a car because it is extending vehicle. If I extend Car by a Bike, then it means Bike is a type of Car, still the exception remains.</p>

<p>Please help me understand what is going on around here.</p>

<p>P.s - please don't take the whole convert car to bike, bike to car literally ;)</p>
