none
What is the oops concept of shadowing in C#?

    Question

  • Hola!

     

    What is the oops concept of shadowing in C#? Is it the same as hiding, using 'new' keyword? How do you choose between 'override' and 'new', as I don't see any difference (apart from the fact that only virtual methods in base class can be overridden in derived class) in the following program:

     

    using System;

     

    class BaseClass
    {
        public virtual string overridingMethod()
        {
            return "BaseClass overridingMethod \n";
        }

        public string hidingMethod()
        {
            return "BaseClass hidingMethod \n";
        }
    }

     

    class DerivedClass : BaseClass
    {
        public override string overridingMethod()
        {
            return "DerivedClass overridingMethod \n";
        }

        public new string hidingMethod()
        {
            return "DerivedClass hidingMethod \n";
        }  
    }

     

    class MainClass
    {
        static void Main()
        {      
           BaseClass bc = new BaseClass();     

           Console.WriteLine(bc.overridingMethod());
        
           Console.WriteLine(bc.hidingMethod());
           
           DerivedClass dc = new DerivedClass();    

           Console.WriteLine(dc.overridingMethod());   

           Console.WriteLine(dc.hidingMethod());
         }
    }

    Friday, August 17, 2007 1:39 PM

Answers

  • Imho, the following code snippet might help you understanding the concept of virtual/overriding and hiding/new better.

    Use an instance of DerivedClass in the context of a BaseClass:

    Code Snippet

    BaseClass bc2 = dc;
    Console.WriteLine(bc2.overridingMethod());
    Console.WriteLine(bc2.hidingMethod());


    Friday, August 17, 2007 3:44 PM

All replies

  • If you make a member virtual it means it can be overrridden and still b able to be usable by the derrived class using the override keyword.  Using the new keyword actually replaces the base member and you cannot use base.MemberName.  With a virtual overriden member you can still access the base implementation.

     

    Hope this helps!

    Friday, August 17, 2007 2:04 PM
  • Imho, the following code snippet might help you understanding the concept of virtual/overriding and hiding/new better.

    Use an instance of DerivedClass in the context of a BaseClass:

    Code Snippet

    BaseClass bc2 = dc;
    Console.WriteLine(bc2.overridingMethod());
    Console.WriteLine(bc2.hidingMethod());


    Friday, August 17, 2007 3:44 PM
  • Cool! But what is shadowing in C#?

    Friday, August 17, 2007 5:19 PM
  • The concept of "Hiding" in c# is the same as the concept of "Shadowing" in vb.net.
    Friday, August 17, 2007 7:04 PM
  • Here is a good example for console app:

        class Program
        {
            static void Main(string[] args)
            {
                A clA = new A();
                B clB = new B();

                Console.WriteLine(clA.Foo()); // output Foo
                Console.WriteLine(clA.Bar()); // output Bar
                Console.WriteLine(clB.Foo()); // output NewFoo
                Console.WriteLine(clB.Bar()); // output OverridenBar

                //now let's cast B to an A class
                Console.WriteLine(((A)clB).Foo()); // output Foo <<<--
                Console.WriteLine(((A)clB).Bar()); // output OverridenBar
                Console.Read();
            }

        }

        class A
        {
            public string Foo() { return "Foo"; }
            public virtual string Bar() { return "Bar"; }
        }
        class B : A
        {
            public new string Foo() { return "NewFoo"; }
            public override string Bar() { return "OverridenBar"; }
        }

     

    • Proposed as answer by j0ck3r Thursday, September 13, 2012 7:59 PM
    Thursday, April 07, 2011 2:46 PM
  • We can simplify a testing code by using a new "dynamic" declaration from Net 4.0

                dynamic d = new B();

                Console.WriteLine(d.Foo()); // output NewFoo
                Console.WriteLine(((A)d).Foo()); // output Foo
                Console.WriteLine(d.Bar()); // output output OverridenBar
                Console.WriteLine(((A)d).Bar()); // output OverridenBar

     

    Thursday, April 07, 2011 7:56 PM
  • It doesn't simplify anything. It's the same output if you declare it as B.
    Friday, April 08, 2011 7:27 AM
  • Here, i am explaining the real difference between Overriding and Shadowing:

    Example:

    (For Overriding)

    Class clsBase

    {

    //as you know, the virtual keyword here to make this method overridable

    public virtual void BaseMethod()

    {

    Console.WriteLine("BaseClass Method called.");

    }

    }

    Class clsDerived:clsBase

    {

    //as you know, the override keyword here to override the base class functionality of the method

    public override void BaseMethod()

    {

    Console.WriteLine("DerivedClass Method called.");

    }

    }

    class Class1

    {

    static void Main()

    {

    clsBase bc=new clsBase();

    bc.BaseMethod();

    clsDerived dc=new clsDerived();

    dc.BaseMethod();

    clsBase cc=new clsDerived();

    cc.BaseMethod();

    }

    }

    o/p:

    BaseClass Method called.

    DerivedClass Method called.

    DerivedClass Method called.

    so, here when we create clsDerived calss object by clsBase class reference, it gives us result as "DerivedClass Method called." which is implementation of derived class clsDerived's  method. This happens  because of overriding.

    -----------------------------------------------------------------------------------------

    Example:

    (For Shadowing)

    Class clsBase

    {

    //as you know, the virtual keyword here to make this method overridable

    public virtual void BaseMethod()

    {

    Console.WriteLine("BaseClass Method called.");

    }

    }

    Class clsDerived:clsBase

    {

    //as you know, the new keyword here for Shadowing

    public new void BaseMethod()

    {

    Console.WriteLine("DerivedClass Method called.");

    }

    }

    class Class1

    {

    static void Main()

    {

    clsBase bc=new clsBase();

    bc.BaseMethod();

    clsDerived dc=new clsDerived();

    dc.BaseMethod();

    clsBase cc=new clsDerived();

    cc.BaseMethod();

    }

    }

    o/p:

    BaseClass Method called.

    DerivedClass Method called.

    BaseClass Method called.

    so, here when we create clsDerived calss object by clsBase class reference, it gives us result as "BaseClass Method called." which is implementation of base class clsBase's  method. This happens purely because of shadowing. This means the derived class maintains both base class, and derived class implementations simultaneously and exposes as desired. The derived class maintains or shadows another implementation without knowledge of the base class.

    I hope the difference between overriding and shadowing is clear. 

    Happy Programming :)

    DEBASISH B

    • Proposed as answer by Dev2011 Thursday, December 13, 2012 10:29 AM
    Thursday, December 13, 2012 10:28 AM