Home:ALL Converter>why i am getting ClassCastException in this case

why i am getting ClassCastException in this case

Ask Time:2012-02-08T18:46:21         Author:user663724

Json Formatter

Please tell me why i am getting ClassCastException in this case

I have type casted , the source of B class to A as shown below , but why i am still getting ClassCastException here .

public class A extends B 
{


}

public class B {
    public String getData() {
        return "B";
    }
}


public class Main {
    public static void main(String args[]) {
        A a = new A();
        B b = new B();
        a = (A) b;
        System.out.println(a.getData());
    }
}

Author:user663724,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/9191784/why-i-am-getting-classcastexception-in-this-case
yy