Overriding Vs Shadowing in C#

Method Overriding

Method overriding is an important feature of OOP that allows us to re-write a base class function or method with a different definition. Overriding is also known as “Dynamic polymorphism” because overriding is resolved at runtime. Here the signature of the method or function must be the same. In other words both methods (base class method and child class method) have the same name, same number and same type of parameter in the same order with the same return type. The overridden base method must be virtual, abstract or override.

Example

public class BaseClass  
{  
    public virtual string GetMethodOwnerName()  
    {  
       return “Base Class”;  
    }  
}  
public class ChildClass : BaseClass  
{  
   public override string GetMethodOwnerName()  
   {  
       return “Child Class”;  
   }  
} 

A method cannot be overriden if:

  • Methods have a different return type
  • Methods have a different access modifier
  • Methods have a different parameter type or order
  • Methods are non virtual or static

Shadowing (method hiding)

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.

Example

Public class BaseClass  
{  
    public string GetMethodOwnerName()  
    {  
       return "Base Class";  
    }  
}  
public class ChildClass : BaseClass  
{  
    public new string GetMethodOwnerName()  
    {  
       return "ChildClass";  
    }  
} 

Test Code

static void Main(string[] args)  
{  
    ChildClass c = new ChildClass();  
    Console.WriteLine(c.GetMethodOwnerName());  
}

Output

Output

If we do not use the new keyword the compiler generates the warning:

compiler generate warning

Mixing Method (Overriding and shadowing (Method Hiding))

We can also use shadowing and method overriding together using the virtual and new keywords. This is useful when we want to further override a method of the child (derived) class.

Example

public class BaseClass  
{  
    public virtual string GetMethodOwnerName()  
    {  
        return "Base Class";  
    }  
}  
public class ChildClass : BaseClass  
{  
    public new virtual string GetMethodOwnerName()  
    {  
        return "ChildClass";  
    }  
}  
public class SecondChild : ChildClass  
{  
    public override virtual string GetMethodOwnerName()  
    {  
        return "Second level Child";  
    }  
} 

Shadowing Vs Overriding

Shadowing Overriding
Shadowing is a VB.Net concept. It also known as method hiding in C#. Using this concept we can provide a new implementation for the base class method without overriding it. Overriding allows us to re-write a base class function with a different definition.
Using the “new” keyword we can do the shadowing or method hiding. C# uses the virtual/abstract and override keyword for method overriding.
Shadowing redefines an entire method or function. Overriding redefines only the implementation of a method or function.
Showing is used to protect against subsequent base class modification. Overriding does polymorphism by defining a different implementation.
We can change the access modifier. We cannot change the access modifier. The access modifier must be the same as in the base class method or function.
There is no control of a base class on shadowing. In other words, a base class element cannot enforce or stop shadowing. The base class has some control over the overriding. Using the keyword abstract, the base class forces the child (derived) class to implement the function or method.
Shadowing an element (function method or property) can be inherited further in a child (derived) class. The shadowed element is still hidden. The same as shadowing, overriding an element is inherited further in a derived class and the overridden element is still overridden.
In shadowing, the signature of an element could be different. In overriding, the signature of the element must be the same.
In shadowing, the base class cannot access the newly created child (derived) class method. This is because the base class has the same name of the element.

In concept, the base class can be accessed using the child object's overridden method.

Shadowing  Example​​​​​​​

static void Main(string[] args)  
{  
    BaseClass c = new ChildClass();  
    Console.WriteLine(c.GetMethodOwnerName());  
} 

Output

Shadow

In overriding, the base class can be accessed using the child object's overridden method.

Overriding Example

static void Main(string[] args)  
{  
    BaseClass c = new ChildClass();  
    Console.WriteLine(c.GetMethodOwnerName());  
}

Output

Overriding

We cannot use the new and override keywords together. If you do then the compiler throws a compilation error.

Output

compilation error

Summary

I hope this article helps you to understand the difference between shadowing (method hiding) and overriding. 


Similar Articles