Why Java object class remains same after casting?

后端 未结 3 713
迷失自我
迷失自我 2021-02-14 23:26

I tried to upcast an objet. But at runtime object class is remained as a derived class.

Derived drv = new Derived();

Base base = (Base) drv;

System.out.print         


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-02-15 00:06

    You can not change the type of an instance in Java. All you're doing with your cast is reference it from a variable of a different type.

提交回复
热议问题