Results 1 to 4 of 4

Thread: Difference between Shadow and override

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2005
    Posts
    12

    Question Difference between Shadow and override

    plz give me the exact differece between shadow and override with example.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Difference between Shadow and override

    Are you talking about "shadowing"? With the use of the keyword "new"? Or are you talking about something else?


    - ØØ -

  3. #3
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190

    Re: Difference between Shadow and override

    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.


    Code:
    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.


    Code:
    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.


    - ØØ -

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Difference between Shadow and override

    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.ki...8/24/1225.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width