Difference between shadowing and overriding in C#.


Shadowing

  • The purpose of Shadowing is to hide the base class method. It uses new keyword in derive class to hide base class method.
  • Example

      public class BaseClass
        {
            public void Print()
            {
                Console.WriteLine("I am Software Developer !!");
            }
        }
        public class DeriveClass : BaseClass
        {
            public new void Print()
            {
                Console.WriteLine("I am Dot Net Expert !!");
            }
        }
     class Program
        {
            static void Main(string[] args)
            {
                DeriveClass objDerive = new DeriveClass();
                objDerive.Print();
                Console.ReadKey();
            }
        }
     


    Output : I am Dot Net Expert !!

  • Access modifier can be different in base class and derive class.

     
        public class DeriveClass : BaseClass
        {
            internal new void Print()
            {
                Console.WriteLine("I am Dot Net Expert !!");
            }
        } 
     

    Output : I am Dot Net Expert !!

  • Base class can’t access the derive class methods.

     
       class Program
        {
            static void Main(string[] args)
            {
                BaseClass objDerive = new DeriveClass();
                objDerive.Print();
                Console.ReadKey();
            }
        }
     


    Output : I am Software Developer !!

overriding

  • The purpose of overriding is to override the base class method. It uses override keyword in derive class and virtual keyword in Base class.
  • Example

    public class BaseClass
        {
            public virtual void Print()
            {
                Console.WriteLine("I am Software Developer !!");
            }
        }
        public class DeriveClass : BaseClass
        {
            public override void Print()
            {
                Console.WriteLine("I am Dot Net Expert !!");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                DeriveClass objDerive = new DeriveClass();
                objDerive.Print();
                Console.ReadKey();
           }
        }
    


    Output: I am Dot Net Expert !!

  • Access modifier should be same for overriding.

        public class DeriveClass : BaseClass
        {
            internal override void Print()
            {
                Console.WriteLine("I am Dot Net Expert !!");
            }
        }   
    


    It will throw an error : cannot change access modifiers when overriding ‘public’ inherited member.

  • Base class can access derive class methods.
    Output : I am Dot Net Expert !!

2 thoughts on “Difference between shadowing and overriding in C#.

  1. Chupacabra

    Suppose you have a base class and you use the base class in all your code instead of the inherited classes, and you use shadow, it will return the values the base class returns instead of following the ineritance tree of the real type of the object.

Comments are closed.