Unlike overriding a method that allows polymorphism, hiding the method using the new
keyword is just a naming issue (and note that using new
just removes the warning that you are hiding something).
In class C
, when you declare:
new public virtual void F() { ... }
Here you define a completely new method that is not related to the superclass F()
, which has the same name.
When an instance of F
assigned to a variable of type A
or B
, a call to F()
using these variables indicates the method defined by the superclass.
Imagine if you didnβt call the C
F()
method, but something like G()
. The following code will not compile:
aG(); bG();
Since with variables statically entered as A
or B
, the compiler cannot see the newly declared method. This is the same situation in your example, except that the superclass is used for the original method named F()
.
source share