Sunday, August 12, 2018

What is Shadowing (Method Hiding).?

Shadowing:-  

A method or function of the base class is available to the child (derived) class without the use of the "overriding" keyword. The compiler hides the function or method of the base class. This concept is known as shadowing or method hiding. In the shadowing or method hiding, the child (derived) class has its own version of the function, the same function is also available in the base class.


Use New  KeyWord to hide the method.

Example:

public class SampleA
    {

        public  void Test()
        {
            Console.WriteLine("This is Example of Test in Base Class");
        }
    }

    public class SampleB:SampleA
    {
        public new void Test()
        {
            Console.WriteLine("This is Example of Test in Derived Class");
        }

    }



In shadowing signature of the method could be different. but in overriding not.








No comments:

Post a Comment