RE: What is the difference between shadow and override...
Shadowing :- This is a VB.Net Concept by which you can provide a new implementation for the base class member without overriding the member. You can shadow a base class member in the derived class by using the keyword Shadows . The method signature access level and return type of the shadowed member can be completely different than the base class member.
Hiding : - This is a C# Concept by which you can provide a new implementation for the base class member without overriding the member. You can hide a base class member in the derived class by using the keyword new . The method signature access level and return type of the hidden member has to be same as the base class member.Comparing the three :-
1) The access level signature and the return type can only be changed when you are shadowing with VB.NET. Hiding and overriding demands the these parameters as same.
2) The difference lies when you call the derived class object with a base class variable.In class of overriding although you assign a derived class object to base class variable it will call the derived class function. In case of shadowing or hiding the base class function will be called.
RE: What is the difference between shadow and override...
In general when you extend a class you shadow fields with the same name in the base class and override virtual methods with the same name and parameter list in the base class. Overriding makes the base class method invisible. Shadowing a field only hides the field from view. You can still explicitly touch the hidden shadowed field if you wish. You cannot touch an invisible overridden method.
RE: What is the difference between shadow and override...
Thre are few differences between those. 1. Shadowing is bad programming practice according to OOPs concepts. 2. In shadowing signature could be different. 3. In Shadowing both Derived class methods and Base Class methods are available for use.(So its a bad practice)
RE: What is the difference between shadow and override
No the VB.NET "shadow" is not exactly the same as the C# "new" override... even though it produces similar behaviour. To see the differences look at the IL generated.
RE: What is the difference between shadow and override
The best way in my opinion to understand the true meaning of the effects of using the "new" modifier in a virtual hierarchy is to think of it like this... The runtime wants to start at the top of the virtual hierarchy and work towards the bottom (towards the most derived class in the chain)... it says "Is there an override in the derived class directly below me?" If so it goes down one level... and then repeats the same question. If the class directly below the current one has applied the "new" modifier to the that virtual chain that method is "hidden"... so the chain stops at the last method that did not have the "new" modifier applied to it.
This happens because the "new" modifier (when applied to a virtual chain) actually creates a brand new v-table entry... thus physically breaking the chain in the hierarchy.
It prints "B" because we start out by creating an actual instance of D that is "pointed to" or referenced using a C. The CallMethod method is actually on class A so we just up to A and then it tries to call "Method" from the top... down. A is virtual... but is overridden by B. It jumps down to B and since C's Method has the "new" modifier applied to it... it cannot "see" the Method instance on class C... thus it stops at B and calls that method. Wild huh!