GeekInterview.com
Series: Subject: Topic:
Question: 300 of 528

Difference between shadow and override?

Asked by: mdchaus | Member Since Nov-2005 | Asked on: Dec 2nd, 2005

View all questions by mdchaus

Showing Answers 1 - 4 of 4 Answers
gheibia

Answered On : Dec 4th, 2005

View all answers by gheibia

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.

  
Login to rate this answer.
Sameeksha

Answered On : 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.CriterionShadowingOverridingPurposeProtecting 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 sequenceRedefined elementAny declared element typeOnly a procedure (Function or Sub) or propertyRedefining elementAny declared element typeOnly a procedure or property with the identical calling sequence1AccessibilityAny accessibilityCannot expand the accessibility of overridden element (for example, cannot override Protected with Public)Readability and writabilityAny combinationCannot change readability or writability of overridden propertyKeyword usageShadows recommended in derived class; Shadows assumed if neither Shadows nor Overrides specifiedOverridable required in base class; Overrides required in derived classInheritance 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
Hector

Answered On : 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
Mnkkapoor

Answered On : Nov 12th, 2008

View all answers by Mnkkapoor

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.

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.