Senator Guerra Souty original series calendar,replica hublot blue steel peach pointer collocation of rolex replica Rome digital scale, track type minute replica watches scale shows that the classical model is swiss replica watches incomparable, wearing elegant dress highlights.
mr-ponna.com

 

YOU ARE HERE: HOME Questions What is difference b w shadowing and using overriding



Shadowing vs Overriding

View(s): 38564

What is difference b/w shadowing and using overriding?

Answer 1)
It is easy to confuse shadowing with overriding. Both are used when a derived class inherits from a base class, and both redefine one declared element with another. But there are significant differences between the two.

You normally use overriding in the following cases:
  • You are defining polymorphic derived classes.
  • You want the safety of having the compiler enforce the identical element type and calling sequence.
You normally use shadowing in the following cases:
  • You anticipate that your base class might be modified and define an element using the same name as yours.
  • You want the freedom of changing the element type or calling sequence.

suppose you have this classes:

class A {
   public int Foo(){ return 5;}
   public virtual int Bar(){return 5;}
}
class B : A{
   public new int Foo() { return 1;}
   public override int Bar() {return 1;}
}

then when you call this:

A clA = new A();
B clB = new B();

Console.WriteLine(clA.Foo()); // output 5
Console.WriteLine(clA.Bar()); // output 5
Console.WriteLine(clB.Foo()); // output 1
Console.WriteLine(clB.Bar()); // output 1

//now let's cast B to an A class
Console.WriteLine(((A)clB).Foo()); // output 5 <<<--
Console.WriteLine(((A)clB).Bar()); // output 1

Suppose you have a base class and you use the base class in all your code instead of the inherited classes, and you use shadow, it will return the values the base class returns instead of following the ineritance tree of the real type of the object.


  Asked in:  Wipro   Expertise Level:  Intermediate
  Last updated on Friday, 21 January 2011
4/5 stars (28 vote(s))

Register Login Ask Us Write to Us Help