samedi 25 août 2018

Casting object dynmically using cast() in java

Can someone explain why this code doesnt work and throw error:incompatible type :Object cannot be converted to aaa

    class aaa{
    }

    class bbb extends aaa{

    }

    class psp{

    public static void main(String a[]){

    bbb aa=new bbb();

    Object o=aa;
    Class z=aaa.class;
    //aaa a1=z.cast(aa); doesnot work
//aaa a1=Class.forName("aaa").cast(o); doesnt work
//aaa a1=Class.forName("aaa").cast(aa);doesnt work
aaa a1=(aaa)o;
a1=(aaa)aa;
 a1=aaa.class.cast(aa);

    }
    }

Can someone tell what i'm missing ?





Aucun commentaire:

Enregistrer un commentaire