Open In App

Difference between Method Overriding and Method Hiding in C#

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding.
In simple words, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and the same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

Example:




// C# program to illustrate the 
// Method overriding concept
using System;
  
// Base class
class My_Parent {
  
    // virtual method
    public virtual void display()
    {
        Console.WriteLine("My new parent class.. !");
    }
}
  
// Derived class
class My_Child : My_Parent {
  
    // Here display method is overridden
    public override void display()
    {
        Console.WriteLine("My new child class.. !");
    }
}
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        My_Parent obj;
  
        // Creating object of the base class
        obj = new My_Parent();
  
        // Invoking method of the base class
        obj.display();
  
        // Creating object of the derived class
        obj = new My_Child();
  
        // Invoking method of derived class
        obj.display();
    }
}


Output:

My new parent class.. !
My new child class.. !

In Method Hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.

Example:




// C# program to illustrate the
// concept of method hiding
using System;
  
// Base Class
public class My_Parent {
  
    public void show()
    {
        Console.WriteLine("This is my parent class.");
    }
}
  
// Derived Class
public class My_Child : My_Parent {
  
    // Hide the method of base class
    // Using new keyword
    public new void show() {
  
        Console.WriteLine("This is my child class.");
    }
}
  
public class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Creating the object of 
        // the derived class
        My_Child obj = new My_Child();
  
        // Access the method of derived class
        obj.show();
    }
}


Output:

This is my child class.

Method Overriding Vs Method Hiding

Method overriding Method hiding
In method overriding, you need to define the method of a parent class as a virtual method using virtual keyword and the method of child class as an overridden method using override keyword. In method hiding, you just simply create a method in a parent class and in child class you need to define that method using new keyword.
It only redefines the implementation of the method. In method hiding, you can completely redefine the method.
Here overriding is an object type. Here hiding is a reference type.
If you do not use override keyword, then the compiler will not override the method. Instead of the overriding compiler will hide the method. If you do not use the new keyword, then the compiler will automatically hide the method of the base class.
In method overriding, when base class reference variable pointing to the object of the derived class, then it will call the overridden method in the derived class. In the method hiding, when base class reference variable pointing to the object of the derived class, then it will call the hidden method in the base class.


Last Updated : 19 Mar, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads