Cast reference of known type to an interface outside of type's hierarchy

后端 未结 6 1343
无人及你
无人及你 2021-01-20 11:44

Say you have a clean class like this:

public class A {
    // Stuff
}

And a interface like this:

public interface G {
    /         


        
6条回答
  •  感动是毒
    2021-01-20 12:09

    This is allowed at compile time, because the Java compiler lets you do this; it assumes you know what you're doing.

    However, the JVM figures it out and throws a ClassCastException at runtime.

提交回复
热议问题