Java Casting Interface to Class

后端 未结 8 1362
闹比i
闹比i 2020-12-05 08:03
public class InterfaceCasting {

    private static class A{}

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


        
8条回答
  •  不知归路
    2020-12-05 08:33

    The detailed rules for compile-time legality of a casting conversion of a value of compile-time reference type S to a compile-time reference type T are as follows:
    [...]
    If S is an interface type:
    - If T is an array type, [...].
    - If T is a type that is not final (§8.1.1), then if there exists a supertype X of T, and a supertype Y of S, such that both X and Y are provably distinct parameterized types, and that the erasures of X and Y are the same, a compile-time error occurs. Otherwise, the cast is always legal at compile time (because even if T does not implement S, a subclass of T might).

    Source :
    JLS : Conversions and Promotions

提交回复
热议问题