Difference between Method Overriding and Method Hiding in C#

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
    }
}

chevron_right


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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.