Difference Between Shadowing And Overriding in OOP Using C#

Introduction:

In this post, we will discuss the concept of Shadowing in OOP using C# and we will see how it works which will give you some idea of where we can use it and hopefully you will be able to decide when working practically in C# where it can be useful.

What is Shadowing:

Shadowing is a concept of OOP (Object Oriented Programming) paradigm. Using Shadowing we can provide a new implementation to base class member without overriding it, which means that original implementation of base class member gets shadowed (hidden) with the new implementation of base class member provided in derived class.

Consider a scenario where you have an external assembly which you have added in your project. You have a class in that assembly and it has a method which is not defined as virtual and you want to override that method (define your own implementation for that method) in the derived class. What would you do?

This is the scenario where you can use shadowing concept to override the method in the derived class.

Definition:

The following are few definitions of it,

According to MSDN:

  • Shadowing is the concept of using Polymorphism in Object Oriented Programming. When two programming elements share the same name, one of them can hide, or shadow, the other one. In such a situation, the shadowed element is not available for reference; instead, when your code uses the element name, the compiler resolves it to the shadowing element.

  • Shadowing is actually hiding overridden method implementation in derived class and call the parent call implementation using derived class object.

Difference between Overriding and Shadowing

There is a major difference in shadowing and overriding which is normally when we override a virtual method in derived class and create an instance of derived class, and then if we hold reference to the derived class object as a base class object, and call that member, it always calls derived class implementation which is supposed to happen. In shadowing the case is different, if for the same virtual member, we shadow it in the derived class using a new keyword and we call the implementation as above, it will call base class implementation when we have reference to an object of type base class. And if we have reference to the same object of derived type it will call derived type implementation, so base class and derived class implementation are hidden from each other. The method of choosing which implementation to be called depends upon if we are calling the member using reference of base type or derived type.

Example:

Suppose, I have a base class BaseLogger which has two virtual methods (which means they can be overridden in subclass) defined as:

  1. public abstract class BaseLogger  
  2. {  
  3.     public virtual void Log(string message)   
  4.     {  
  5.         Console.WriteLine("Base: " + message);  
  6.     }  
  7.   
  8.     public virtual void LogCompleted()  
  9.     {  
  10.         Console.WriteLine("Completed");  
  11.     }  
  12. }  
Now I create a class Logger that inherits from BaseLogger class. Logger class looks like this,
  1. public class Logger: BaseLogger  
  2. {  
  3.     public override void Log(string message)   
  4.     {  
  5.         Console.WriteLine(message);  
  6.     }  
  7.   
  8.     public new void LogCompleted()   
  9.     {  
  10.         Console.WriteLine("Finished");  
  11.     }  
  12. }  
Now I want my Console to print the following lines:

 

  • Log started!
  • Base: Log Continuing
  • Completed

What should be written inthe main program to get the above output? Here is the code we will need to write:

  1. public class Program  
  2. {  
  3.     public static void Main()   
  4.   {  
  5.   
  6.         BaseLogger logger = new Logger();  
  7.   
  8.         logger.Log("Log started!");  
  9.         logger.Log("Base: Log Continuing");  
  10.         logger.LogCompleted();  
  11.     }  
  12. }  
The first call to Log method is OK, it has called Derived Logger class Log method, and it should, because we have overridden it in the Logger class.

The second call is also the same.

Now note the third call, it has called the base class LogCompleted() method, not derived class, it is because we have defined the derived class method with a new keyword which is hiding the derived class method when we are calling it with object of type BaseLogger, which is our base class.

If we want to call the derived class LogCompleted() method our object reference should be of type Logger not BaseLogger, while in method overriding this is not the case.

In method overriding, if we cast the object of derived class to base class and call method, it will call overridden implementation of derived class.
 
Read more articles on C#:

 

Next Recommended Readings