Overriding Vs Shadowing in C#

Introduction

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

  1. public class BaseClass  
  2. {  
  3.     public virtual string GetMethodOwnerName()  
  4.     {  
  5.        return “Base Class”;  
  6.     }  
  7. }  
  8. public class ChildClass : BaseClass  
  9. {  
  10.    public override string GetMethodOwnerName()  
  11.    {  
  12.        return “Child Class”;  
  13.    }  

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

  1. Public class BaseClass  
  2. {  
  3.     public string GetMethodOwnerName()  
  4.     {  
  5.        return "Base Class";  
  6.     }  
  7. }  
  8. public class ChildClass : BaseClass  
  9. {  
  10.     public new string GetMethodOwnerName()  
  11.     {  
  12.        return "ChildClass";  
  13.     }  

Test Code

  1. static void Main(string[] args)  
  2. {  
  3.     ChildClass c = new ChildClass();  
  4.     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
  1. public class BaseClass  
  2. {  
  3.     public virtual string GetMethodOwnerName()  
  4.     {  
  5.         return "Base Class";  
  6.     }  
  7. }  
  8. public class ChildClass : BaseClass  
  9. {  
  10.     public new virtual string GetMethodOwnerName()  
  11.     {  
  12.         return "ChildClass";  
  13.     }  
  14. }  
  15. public class SecondChild : ChildClass  
  16. {  
  17.     public override virtual string GetMethodOwnerName()  
  18.     {  
  19.         return "Second level Child";  
  20.     }  

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

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

Output

Shadow

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

Overriding Example

  1. static void Main(string[] args)  
  2. {  
  3.     BaseClass c = new ChildClass();  
  4.     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. 

Up Next
    Ebook Download
    View all
    Learn
    View all