Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is C# Method Overriding?
2.1.
Example
3.
Frequently Asked Questions
3.1.
What is method overriding?
3.2.
Why we use method overriding in C#?
4.
Conclusion
Last Updated: Mar 27, 2024
Easy

C# Method Overriding

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Like the polymorphism and method hiding concepts in C#, there is also a concept that allows the invoking of functions from a base class in the derived class. This concept is known as Method Overriding. In this article, we will learn about the C# language's method overriding concepts. Let us dive deeper into the topic.

What is C# Method Overriding?

When a method defined in the parent class has a specific implementation in the child class or the creation of a method in the child class with the same definition as the method in the parent class is called Method Overriding. To override a parent class method, the child-class method should either have the same parameters, signature, return type, or sub return type. Runtime Polymorphism or Dynamic Polymorphism is achieved using Method Overriding.

Example

class base_class
{
    public void ninja();
}
class derived_class : base_class
{
    public void ninja();
}
class Main_Method
{
 static void Main()
 {
    derived_class d = new derived_class();
    d.ninja();
 }
}

 

Check out this article - Compile Time Polymorphism

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is method overriding?

When a method defined in the parent class has a specific implementation in the child class or the creation of a method in the child class with the same definition as the method in the parent class is called Method Overriding. 

Why we use method overriding in C#?

If derived class defines same method as defined in its base class, it is known as method overriding in C#. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of the method which is already provided by its base class.

Conclusion

In this article, we have extensively discussed we will learn about the C# language's method overriding concepts. We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Do upvote our blog to help other ninjas grow.

Happy Learning! 

Previous article
C# Method Parameters
Next article
C# Method Hiding
Live masterclass