Table of contents
1.
Introduction
2.
Method Overriding
3.
Method Hiding
4.
Method Overriding VS Method Hiding
5.
Frequently Asked Questions
5.1.
What is Method Overriding?
5.2.
What is Method Hiding?
5.3.
What is the reference type of Method Overriding and Method Hiding?
6.
Conclusion
Last Updated: Mar 27, 2024

Method Overriding vs Method Hiding in C#

Author NISHANT RANA
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C# is the modern Object-Oriented Programming Language. It is very similar to the java programming language. It is developed by Microsoft and the first version was released in 2002. The latest version of C# is 8.0. In this blog, we will be discussing Method Overriding and Method Hiding and the difference between them.

Method Overriding

It is one of the most powerful techniques in C#. Method overriding is the technique using which one can implement the functions of the base class in the derived class. One can update the implementation, usage, and behaviors of the functions of the parent class in the derived class. Using this technique, Runtime Polymorphism is implemented in C#.

In the above picture, we can see that the BMW class is the derived class and the Vehicle class is the parent class. We are overriding the numberOfTyres() and honk() function in the BMW class.

using System;
  
// Base class
class Vehicle {
  
    // virtual method
    public virtual void numberOfTyres()
    {
        Console.WriteLine("A vehicle can have 2, 3, 4, 6, 8 tyres");
    }
    public virtual void honk()
    {
        Console.WriteLine("A vehicle is honking");
    }
    public void accelerate()
    {
        Console.WriteLine("Vehicle is accelerating");
    }
}
  
// Derived class
class BMW : Vehicle {
  
    // Here numberOfTyres method is overridden
    public override void numberOfTyres()
    {
        Console.WriteLine("BMW has 4 tyres");
    }
    public override void honk()
    {
        Console.WriteLine("BMW is honking");
    }
}
  
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        Vehicle obj;
  
        // Creating object of the base class
        obj = new Vehicle();
  
        // Invoking method of the base class
        obj.numberOfTyres();
  
        // Creating object of the derived class
        obj = new BMW();
  
        // Invoking method of derived class
        obj.numberOfTyres();
    }
}

Output

A vehicle can have 2, 3, 4, 6, 8 tyres
BMW has 4 tyres

Method Hiding

Method hiding is the concept using which a user can hide the details/implementation of various methods of base class in the derived class. So, basically one can redefine the implementation of the method of the base class in the derived class. We can do this using a new keyword.

using System;
  
// Base class
class Vehicle {


    public void numberOfTyres()
    {
        Console.WriteLine("A vehicle can have 2, 3, 4, 6, 8 tyres");
    }
}
  
// Derived class
class BMW : Vehicle {
  
    // Here numberOfTyres method is hidden
    public new void numberOfTyres()
    {
        Console.WriteLine("BMW has 4 tyres");
    }
}
  
class Main {
  
    // Main Method
    public static void Main()
    {
  
        // Creating object of the derived class
        BMW obj = new BMW();
  
        // Invoking method of derived class
        obj.numberOfTyres();
    }
}

Output

BMW has 4 tyres

Method Overriding VS Method Hiding

S. No. Method Overriding Method Hiding
1 In method overriding, the methods of the base class can be re-implemented in the derived class. Whenever we call this method using the derived class, the new implementation is invoked. Method hiding is the concept using which a user can hide the details/implementation of various methods of base class in the derived class using the new keyword.
2 We define the function in the base class using the virtual keyword, and the method in the derived class using the override keyword. We define the function in the derived class using the new keyword.
3 It just redefines the method. It can completely redefine the method.
4 When the parent class reference variable points to the child class, then the method in the child class is invoked while calling. When the parent class reference variable points to the child class, then the hidden method in the parent class is invoked while calling.
5 Implementation type of overridden method is the object type. Implementation type of the hidden method is the reference type.

Check out this article - Compile Time Polymorphism

 Also Read About - singleton design pattern in c#

Frequently Asked Questions

What is Method Overriding?

Method overriding is the technique using which one can implement the functions of the base class in the derived class. 

What is Method Hiding?

Method hiding is the concept using which a user can hide the details/implementation of various methods of base class in the derived class.

What is the reference type of Method Overriding and Method Hiding?

The implementation type of overridden method is the object type, whereas the implementation type of the hidden method is the reference type.

Conclusion

In this article, we have extensively discussed Method Overriding and Method Hiding. We start with a brief introduction to Method Overriding and Method Hiding, then discussed the difference between both.

Recommended Reading:

Difference Between Data Analyst and Business Analyst

After reading this blog, you will get everything about the difference between Method Overriding and Method Hiding, are you not feeling excited to read/explore more articles on the topic of file systems? Don't worry; Coding Ninjas has you covered. To learn, see Methods in C#C# polymorphism.

You can refer to the Data Structures and AlgorithmsJavaScript courses which can upskill you. You can discover more such courses by referring to the Guided Path on Coding Ninjas Studio. You can also test your knowledge by taking up the mock test and contests which are held by Code studio.

If you are about to interview with companies like Google, Adobe, Amazon, etc. then you can refer to problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

 

Live masterclass