Why can't reference to child Class object refer to the parent Class object?

后端 未结 14 1118
执念已碎
执念已碎 2020-12-17 10:50

I was explaining OOP to my friend. I was unable to answer this question. (How shameful of me? :( )

I just escaped by saying, since OOP depicts the real world. In rea

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-17 11:26

    I think you mean:

    Child aChild = aParent;
    

    You did not specify that aChild is of type Child.

    The reference to a Child type will mean that you can call members on it that may no exist in the Parent. So, if you assign a Parent object to a Child reference, you will be able to call members that do not exist on the Parent.

提交回复
热议问题