This is a discussion on Shadow and override within the C# Programming forums, part of the Software Development category; Hui Guys can anyone tell me the Difference between shadow and override?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| hey, 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. Shadowing Protecting against a subsequent base class modification introducing a member you have already defined in your derived class. Any declared element type Any declared element type Any accessibility Any combination Shadows recommended in derived class; Shadows assumed if neither Shadows nor Overrides specified. Shadowing element inherited by further derived classes; shadowed element still hidden2. Overriding Achieving polymorphism by defining a different implementation of a procedure or property with the same calling sequence Only a procedure (Function or Sub) or property Only a procedure or property with the identical calling sequence Cannot expand the accessibility of overridden element (for example, cannot override Protected with Public) Cannot change readability or writability of overridden property Overridable required in base class; Overrides required in derived class. Overriding element inherited by further derived classes; overridden element still overridden
__________________ Venkat knowledge is Power |
| |||
| hi, 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
__________________ H2O Without us, no one can survive.. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Is it possible to override a method in Partial class ? | oxygen | ASP and ASP.NET Programming | 2 | 02-08-2008 02:47 AM |
| shadow and override | sivakumar | C# Programming | 2 | 08-17-2007 05:58 AM |
| Difference between New and Override keywords | raja | C# Programming | 2 | 08-17-2007 04:52 AM |
| Drop shadow effect using css | muthukumar | HTML, CSS and Javascript Coding Techniques | 0 | 07-17-2007 12:43 PM |