Overriding methods in java and then casting object to parent class behavior

This is basis of polymorphism

And it is supposed to work like that.

Any method is dispatched (selected/invoked) dynamically according to the actual type of the object in stead of the type by which it is being referred to.

When you cast the object to another type, you just refer it using another type. The actual type of the object is not changed. (And it can never change).

So the behavior that you are observing is as expected and it is designed to be that way. It’s definitely not a limitation.

Hope this helps.

Leave a Comment