GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 355 of 436    Print  
What is the difference between shadow and override
override id used for variables and methods but override only used for methods.


  
Total Answers and Comments: 7 Last Update: January 26, 2008     Asked by: Pradeep 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
July 26, 2006 10:43:38   #1  
ramki83 Member Since: July 2006   Contribution: 2    

RE: What is the difference between shadow and override...
overriding is used to redefines only the methods.but shadowing redefines the entire element.
 
Is this answer useful? Yes | No
August 04, 2006 02:27:52   #2  
Ashish Tandon        

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.

 
Is this answer useful? Yes | No
September 13, 2006 18:37:16   #3  
prudhviram Member Since: September 2006   Contribution: 11    

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.
 
Is this answer useful? Yes | No
February 12, 2007 06:55:34   #4  
Rajesh K        

RE: What is the difference between shadow and override...
Is the shadow is same like virtual keyword in C#.net...?
 
Is this answer useful? Yes | No
November 21, 2007 02:56:03   #5  
Ram Kinkar Pandey        

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)

Does it helps!!


 
Is this answer useful? Yes | No
January 25, 2008 12:58:36   #6  
whbloodworth Member Since: January 2008   Contribution: 4    

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.
 
Is this answer useful? Yes | No
January 25, 2008 13:07:29   #7  
whbloodworth Member Since: January 2008   Contribution: 4    

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.

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


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape