Difference between shadow and override?

Questions by mdchaus

Showing Answers 1 - 14 of 14 Answers

gheibia

  • Dec 4th, 2005
 

shadowing hide the inherrited method using "new" keyword and CLR choose the target mthod between the parent and child to call using the object's runtime type.Overriding hide the inherrited method using override keyword and the parent should be virtual. overrding always choose the object's compile-time type.

  Was this answer useful?  Yes

Sameeksha

  • Dec 5th, 2005
 

I find this table from MSDN to be useful to explain differences between shadowing and overriding: The main constraint on overriding is that it needs permission from the base class with the 'overridable' keyword. Shadowing does not require permission of base class.

CriterionShadowingOverriding
PurposeProtecting against a subsequent base class modification introducing a member you have already defined in your derived classAchieving polymorphism by defining a different implementation of a procedure or property with the same calling sequence
Redefined elementAny declared element typeOnly a procedure (Function or Sub) or property
Redefining elementAny declared element typeOnly a procedure or property with the identical calling sequence1
AccessibilityAny accessibilityCannot expand the accessibility of overridden element (for example, cannot override Protected with Public)
Readability and writabilityAny combinationCannot change readability or writability of overridden property
Keyword usageShadows recommended in derived class; Shadows assumed if neither Shadows nor Overrides specifiedOverridable required in base class; Overrides required in derived class
Inheritance of redefining element by classes deriving from your derived classShadowing element inherited by further derived classes; shadowed element still hidden2Overriding element inherited by further derived classes; overridden element still overridden

Hector

  • Nov 14th, 2006
 

This is wrong:shadowing hide the inherrited method using "new" keyword and CLR choose the target mthod between the parent and child to call using the object's runtime type.Overriding hide the inherrited method using override keyword and the parent should be virtual. overrding always choose the object's compile-time type.The right this is the opossite:shadowing hide the inherrited method using "new" keyword and CLR choose the target mthod between the parent and child to call using the object's compile-time type..Overriding hide the inherrited method using override keyword and the parent should be virtual. overrding always choose the object's run-time type.public class Base{ public virtual void SomeMethod() { }}public class Derived : Base{ public override void SomeMethod() { }}// override works with the object run-time type Base b = new Derived();b.SomeMethod();It will execute Derived.SomeMethod because b is type Derived on runtime type.try MessageBox.Show(b.GetType().ToString()) to proof it.Now instead:public class Base{ public virtual void SomeOtherMethod() { }}public class Derived : Base{ public new void SomeOtherMethod() { }}...Base b = new Derived();Derived d = new Derived();b.SomeOtherMethod();d.SomeOtherMethod();Will first call Base.SomeOtherMethod, because b is Base type at compile time and Derived type at runtimethen Derived.SomeOtherMethod because d is Base type at compile time and Derived type at runtime

Mnkkapoor

  • Nov 12th, 2008
 

In shadowing the Access Level , Return Type and Signature of the method can change in the drived class where as in overriding access level, return type and signature of the method has to be same.

  Was this answer useful?  Yes

Saket Choubey

  • Dec 29th, 2014
 

Shadowing hides the base class method from the derived class method by using new keyword. Purpose of shadowing is not to override a method but to give a new implementation.

  Was this answer useful?  Yes

Pooja

  • Apr 1st, 2015
 

Overriding is redefining an existing virtual method from the base class while shadowing is creating a new method with same name and same signature as in the base class

  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