If A extends B extends C, why can I cast to A but get a ClassCastException casting to C?

▼魔方 西西 提交于 2019-12-05 05:17:55

Android has a modified class hierarchy here, see comment in http://www.netmite.com/android/mydroid/1.5/dalvik/libcore/security/src/main/java/org/bouncycastle/asn1/ASN1Sequence.java Are you absolutely sure the version you are using that a DERSequence is a subtype of ASN1Object?

e.g it is here http://www.eecs.berkeley.edu/~jonah/bc/org/bouncycastle/asn1/DERSequence.html

but not here http://www.androidjavadoc.com/m3-rc37a/org/bouncycastle/asn1/DERSequence.html

Can you try executing this:

package classtest;

import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.DERSequence;

public class A {

    public static void main(String[] args) {
        testCast(new DERSequence());
    }

    private static void testCast(Object o) {
        DERSequence o2 = (DERSequence) o;
        ASN1Sequence o3 = o2;
        ASN1Object o4 = o3;

        ASN1Object o5 = (ASN1Object) o;
    }

}

(for me, this does not throw any exception)

If this does not work, you should check the answer of vickirk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!