When programming, we usually use some type casting operations.
When casting happens on objects “on the same level”, it feels good. But when this happens at the objects "at different levels" (mainly between father and son), it seems strange.
Considering this:
Class Son extends Father
When Son s = (Son)father;, this is completely unreasonable. Since the "Son" is no longer the "Father", the "Son" can grow with some new properties that the "Father" does not have, the casting operation makes these properties unknown.
On the other hand, it Father f = (Father)sonseems reasonable, but according to the LSP
"An instance of a derivative must be able to replace any instance of its superclass."
The “Son” can do whatever his “Father” can do, so the casting operation seems futile.
So, can I say that these casting operations are an Oais design principle, but necessary?
source
share