Method Overriding VS Method Hiding

My previous two articles explained method hiding and method overriding.

In this article we will see the differences between the two with an example.

In my project, I created a ParentClass with a method named “ParentMethod”. I have also created a child class named “ChildClass” that is inheriting from the parent class.

  1. using System;  
  2.   
  3. namespace MethodOverridingMethodHiding {  
  4.     class ParentClass {  
  5.         public void ParentMethod() {  
  6.             Console.WriteLine("I am a parent method");  
  7.         }  
  8.     }  
  9.   
  10.     class ChildClass : ParentClass {  
  11.   
  12.     }  
  13. }  
Let's look at method overriding first

To override a base class method, we need to make that method “virtual”.

 

  1. class ParentClass {  
  2.         public virtual void ParentMethod() {  
  3.             Console.WriteLine("I am a parent method");  
  4.         }  
  5.     }  
To override the method in our child class, we type override then space then select the method and press Enter.

override

It will generate the following code.
  1. class ChildClass : ParentClass {  
  2.      public override void ParentMethod() {  
  3.          base.ParentMethod();  
  4.      }  
  5.  }  
We can remove the default implementation and provide our own.
  1. class ChildClass : ParentClass {  
  2.     public override void ParentMethod() {  
  3.         Console.WriteLine("I am overriding the parent class method");  
  4.     }  
  5. }  
The next step is to create an instance of our class by using a base class reference variable pointing to the child class object in our main method.
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         ParentClass pc = new ChildClass();  
  4.         pc.ParentMethod();  
  5.     }  
  6. }  
Run the application

application

Now let's look at an example where we will hide the base method rather than overriding it.

First, we need to remove the virtual keyword from the ParentMethod.
  1. class ParentClass {  
  2.     public void ParentMethod() {  
  3.         Console.WriteLine("I am a parent method");  
  4.     }  
  5. }  
Second, we need to remove the override keyword and add a new keyword.
  1. class ChildClass : ParentClass {  
  2.     public new void ParentMethod() {  
  3.         Console.WriteLine("I am overriding the parent class method");  
  4.     }  
  5. }  
In our main method, we will again create an instance of our ChildClass class by creating a base class reference variable pointing to the child class.
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         ParentClass pc = new ChildClass();  
  4.         pc.ParentMethod();  
  5.     }  
  6. }  
Run the program

The hidden parent class method will be invoked.

Run the program

Note

But if you want to invoke the child class method, we can do the following in the main method:
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         ChildClass cc = new ChildClass();  
  4.         cc.ParentMethod();  
  5.     }  
  6. }  
output

From the preceding two examples it is very clear that in method overriding, a base class reference variable pointing to a child class object will invoke the overridden method in the child class and in method hiding, a base class reference variable pointing to a child class object will invoke the hidden method in the base class.


Similar Articles