Click to See Complete Forum and Search --> : Difference between Shadow and override
PankajGupta
Sep 22nd, 2005, 06:33 AM
plz give me the exact differece between shadow and override with example.
NoteMe
Sep 22nd, 2005, 06:48 AM
Are you talking about "shadowing"? With the use of the keyword "new"? Or are you talking about something else?
- ии -
NoteMe
Sep 22nd, 2005, 07:03 AM
Ok, since you didn't get the time to answer before I had woooppped togheter some code and some text, I will take a guess..:) Ask again if I didn't answer your question.
The keyword override replaces the method in the base class with a new one in the derived class. With new, you just hides it. This means that you can NOT in any way call the base class method if you have all ready overrided it. But if you have just shadowed it (used the word new), then you have just made a new one to hide the one in the base class. You can still call the function in the base plass by specifying it with the name of the base class. Here is an example for you. Look at these 3 classes. One Base class called ParentClass. Then two clases that inherits from that class. Now, one is overriding the function in the base class, while the other one hides/shadows it.
using System;
public class parentClass{
public virtual void Method(){
Console.WriteLine("This is Parent");
}
}
public class ChildOverride : ParentClass {
public override void Method() {
Console.WriteLine("This is Override");
}
}
public class ChildNew : ParentClass {
public new void Method() {
Console.WriteLine("This is New");
}
}
If we now try to make an object of the two derived classes and call their Method(), then lets see what functions it calls.
ChildOverride co = new ChildOverride();
ChildNew cn = new ChildNew();
co.Method();
((ParentClass)co).Method();
cn.Method();
((ParentClass)cn).Method();
Output:
This is Override
This is Override
This is New
This is Parent
As you can see. There is no way for the OverrideClass to call the method in the Base class. But for the NewClass it is possible by specisfying so. This will add some extra overhead to your class, since it has to maintain both versions of the function. So you have to think through what you want to do. If there is no use for the base class funtion at all in the inherited class, then override it. If there is some use, then shadow it.
- ии -
jmcilhinney
Sep 22nd, 2005, 06:31 PM
Assuming that shadowing and overriding work the same way as they do in VB.NET, you might like to read the following article: http://codebetter.com/blogs/grant.killian/archive/2003/08/24/1225.aspx
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.