Java Casting Interface to Class

Viewed 33442
public class InterfaceCasting {

    private static class A{}

    public static void main(String[] args) {
        A a = new A();
        Serializable serializable = new Serializable(){};
        a = (A)serializable;
    }

}

Compilation succeed but Runtime exception

Exception in thread "main" java.lang.ClassCastException: InterfaceCasting$1 cannot be cast to InterfaceCasting$A

WHY COMPILATION SUCCEED? Compiler must known that serialiazable is not A?

8 Answers
Related