Why Java object class remains same after casting?

后端 未结 3 714
迷失自我
迷失自我 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-14 23:49

    An upcast does not change the object's type. As a matter of fact, NOTHING changes a Java object's type.

    That's the very core of OO programming: An object has a defined behavior that can't be influenced from the outside.

提交回复
热议问题