Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Overriding Vs Shadowing in C#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 490
    Comment on it

    Overriding Vs Shadowing in C#

     

    Method overriding

     

    • A base class method re-written in derived class with different definition is method overriding.

     

    • At run time overriding is resolved therefore it is also known as “Dynamic Polymorphism”.

     

    • In overriding methods (base class method and derived class method) should have the same name, same type and same number of parameter and in the same order with the same return type therefore method declaration is same only definition changes.

     

    • virtual, abstract or override keywords to be used with overridden base method.

     

    Example to demonstrate method overriding

     

    using System;
    
    namespace DemoApplication
    {
        public class Baseclass
        {
            // Method with virtual keyword can be overriden
            public virtual string TestMethod()
            {
                return "Base Class Method called";
            }
        }
        public class Derived : Baseclass
        {
            // TestMethod is overridden with override keyword
            public override string TestMethod()
            {
                return "Derived Class Method called";
            }
        }
        class TestMethodOverriding
        {
            public static void Main(string[] args)
            {
                //An instance of base class is created
                Baseclass baseclass = new Baseclass();
    
                //baseclass TestMethod will be called
                Console.WriteLine(baseclass.TestMethod());
    
                //An instance of derived class is created
                Derived derived = new Derived();
    
                //baseclass instance assigned with derived class object
                baseclass = derived;
    
                //derived class TestMethod will be called
                Console.WriteLine(baseclass.TestMethod());
    
                Console.ReadKey();
    
            }
        }
    }


    Output

     

     

    In above program overriding is resolved at run time since object assignment to an instance of base class decides whose(base or dervived class) TestMethod() to be called.

     

    Method Shadowing

     

    • A base class method re-written in derived class with not only different definition but also different declaration only method name remain same is method Shadowing.

     

    • The base class provides method or function to the child (derived) class and derived class uses this function via new keyword.

     

    • The base class method is hidden by the compiler when derived class make use of new keyword in method.

     

    • Method overriding and Shadowing can be used together using the virtual and new keywords together. This is useful whenever there is a requirement of overriding a method of the child (derived) class.

     

    • The compiler throws a compilation error if new and override keywords are used together.

     

    Example to demonstrate method shadowing

     

    using System;
    
    namespace DemoApplication
    {
        public class Baseclass
        {
            // Method with virtual keyword can be overriden
            public string TestMethod()
            {
                return "Base Class Method called";
            }
        }
        public class Derived : Baseclass
        {
            // Dervide.TestMethod() hides inherited member Baseclass.TestMethod() but with usage of new keyword,method shadowing takes place 
            public new string TestMethod()
            {
                return "Derived Class Method called";
            }
        }
        class TestMethodShadowing
        {
            public static void Main(string[] args)
            {
                //An instance of base class is created
                Baseclass baseclass = new Baseclass();
    
                //baseclass TestMethod will be called
                Console.WriteLine(baseclass.TestMethod());
    
                //An instance of derived class is created
                Derived derived = new Derived();
    
                //derived class TestMethod will be called
                Console.WriteLine(derived.TestMethod());
    
                Console.ReadKey();
    
            }
        }
    }

     

    Output

     

     

    In above program both instances will call their own version of TestMethod(), method shadowing is resolved at compile time.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: