29th
September
2019
Blog
Overriding vs Shadowing
Hi, in short, I’d like to describe why using method shadowing in a project can be dangerous.
First, let’s have a short introduction to what method overriding and shadowing is.
Overriding – it’s changing the method body. What’s important is that the method signature is resolved at runtime based on the object’s type. The base method has to be declared with ‘virtual’ modifier.
Shadowing – it’s changing the method body. It’s early bound, thus resolved at compile type based on the object’s type. The base method doesn’t have to have any special modifier. However, in the derived class, the shadowing method can optionally include ‘new’ modifier (it’s not necessary, but without it compilers generate a warning).