What is the difference between shadow and override

Override id used for variables and methods but override only used for methods.

Showing Answers 1 - 15 of 15 Answers

Ashish Tandon

  • Aug 4th, 2006
 

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.

  Was this answer useful?  Yes

prudhviram

  • Sep 13th, 2006
 

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.

  Was this answer useful?  Yes

Rajesh K

  • Feb 12th, 2007
 

Is the shadow is same like virtual keyword in C#.net...?

  Was this answer useful?  Yes

Ram Kinkar Pandey

  • Nov 21st, 2007
 

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)

Does it helps!!

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

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.

Take this example:

class A

{

   public virtual void CallMethod () { Method(); }   protected virtual void Method() { Console.WriteLine("A"); }

}

class B : A

{

   protected override void Method() { Console.WriteLine("B"); }

}

class C : B

{

   protected new virtual void Method() { Console.WriteLine("C"); }

}

class D : C

{

   protected override void Method() { Console.WriteLine("D"); }

}

class Program

{

   static void Main(string[] args)

   {

      C objeckt = new D();

      objeckt.CallMethod();

   }

}

What is printed?  The answer is "B".  Why?

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!

Enjoy!

- Wil

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions