Object slicing refers to the loss of information from a derived class instance when it is copied to a parent class instance, due to the nature of value types that assign values to the stack instead of the heap. This is a bug that occurs in languages like Java, which only have reference types that allocate values to the heap.
Upcasting should not be used for value types due to the issue of object slicing. In most cases where upcasting is needed, there is already an issue with the code that needs to be fixed. If upcasting must be used under certain circumstances, values must be assigned to the heap.
This article is a translation of a Korean post written in 2015. If you would like to view the original, please refer to this link.
Comments
Post a Comment