Method overriding in c#, Method overloading in c#, difference between method overloading and method overriding in c#, method overloading vs method overriding in c#. those questions are often asked in programming interviews therefore let’s practice them in this post.
Welcome back visitors, In beginner-level interviews, candidates are often asked about ‘method overloading’ and ‘method overriding’ also known as ‘function overloading’ and ‘function overriding’ in programming. Understanding these concepts is crucial as they form the backbone of object-oriented programming (OOP) principles in C#. This article will guide you through both concepts, providing clear examples and explanations.
FREE Offer: If you’re a new visitor preparing for an interview and need help, you can use the form on the right side to ask your question. It’s free and doesn’t require signing in. Alternatively, you can ask in our Quora space, which is also free, and you’ll receive a response within 24 hours. Go ahead and check it out! Don’t miss our top tips on Inheritance in OOPs: Ultimate Guide for Coding Interviews, best way to find duplicates in an array , reverse a linked list recursively and Function Overloading in C++. Your dream job is just a click away!
If you are Unity Developer and instructed to learn ECS and DOTs check my new blog. where I will convert Non-ECS unity project to ECS project.
Table of Contents
Understand Method Overloading
Before diving into the programming part and exploring examples of function overloading, let’s first understand the definition.
Method Overloading allows a class to have multiple methods with the same name but different parameters. This is also known as compile-time polymorphism.
Example:
Imagine we have a class called Calculator. This class has several Add methods: one to add integers, another to add doubles, and even one to add arrays. This way, no matter what you need to add, the Calculator has got you covered.
Just keep your focus on method name ‘Add’ all methods have same name but deferent data types in parameters if you still not able to understand please follow this article where I have mentioned function overloading in depth with example.
public class Calculator
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
public int Add(int[] numbers) => numbers.Sum();
}
Understand Method Overriding
Imagine you have a class called Animal with a method called makeSound(). Now, if you create a subclass called Dog that also has a makeSound() method, the Dog’s version will be used when you call makeSound() on a Dog object.
Method Overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is known as runtime polymorphism.
Example:
Imagine we have a base class called Animal with a method called MakeSound. Now, suppose we have a derived class called Dog that has its own version of the MakeSound method.
Method Overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This is known as runtime polymorphism.
Let’s implement function overriding in programming.
public class Animal
{
public virtual void MakeSound() => Console.WriteLine("Animal sound");
}
public class Dog : Animal
{
public override void MakeSound() => Console.WriteLine("Bark");
}
In this example, the Animal class has a method called MakeSound that prints ‘Animal sound’. The virtual keyword is used to indicate that this method can be overridden in a derived class.
Now, we have a derived class called Dog that has its own version of the MakeSound method. By using the override keyword, the Dog class tells the compiler that it is providing a specific implementation of the MakeSound method that is already defined in its superclass, Animal.
So, when you call the MakeSound method on an instance of Dog, it will print ‘Bark’, demonstrating how method overriding works. This allows the Dog class to provide its own specific behavior for the MakeSound method, which is different from the base class Animal.
Differences Between Method Overloading and Method Overriding
Method Overloading | Method Overriding | |
Purpose | To add more methods with the same name but different parameters | To change the behavior of an inherited method |
Polymorphism Type | Compile-time polymorphism | Runtime polymorphism |
Keywords | No special keyword required | Uses override keyword in the derived class and virtual keyword in the base class |
I trust that you’ve grasped the distinctions outlined in the table regarding method overloading and method overriding. However, if the table proved to be unclear, there’s no cause for concern. I’ll be offering further elucidation through additional, real-world examples below.
So, think of method overloading like having multiple options at a restaurant menu with the same dish name but different ingredients or preparation methods. It’s all about adding more methods with the same name but with different parameters. This is handy when you want to perform similar actions but with varying data types or number of parameters. No fancy keywords needed here, just define your methods with different parameters, and you’re good to go!
On the other hand, method overriding is like inheriting your mom’s recipe but giving it your own twist. It’s all about changing the behavior of a method that you’ve inherited from a superclass in your subclass. This gives you the flexibility to customize the functionality to suit your subclass’s specific needs. To do this, you use the override keyword in your subclass to indicate that you’re providing a new implementation of a method already defined in the superclass, which was marked with the virtual keyword. It’s like saying, “Hey, superclass, I’m changing things up a bit here!
Examples of Method Overloading in C#
Overloading methods in a MathOperations class to handle different data types.
public class MathOperations
{
public int Multiply(int a, int b) => a * b;
public double Multiply(double a, double b) => a * b;
public decimal Multiply(decimal a, decimal b) => a * b;
}
Examples of Method Overriding in C#
Overriding methods in a Vehicle class hierarchy.
public class Vehicle
{
public virtual void StartEngine() => Console.WriteLine("Engine started");
}
public class Car : Vehicle
{
public override void StartEngine() => Console.WriteLine("Car engine started");
}
Summarizing Method Overloading vs Method Overriding
In summary, method overloading and method overriding are fundamental concepts in C# that facilitate polymorphism. Overloading enables multiple methods with the same name but different parameters within the same class, while overriding allows a subclass to modify the behavior of an inherited method. Understanding these concepts is essential for writing flexible and reusable code in object-oriented programming.
By following these steps, you can effectively distinguish and implement method overloading and method overriding in C#.
Amazing! I just read your blog post, and I’m blown away. Your analysis on this subject is spot-on. I’ve learned so much and am eager to see what you write next. Keep up the great work!
Great information shared.. really enjoyed reading this post. Thank you, author, for sharing this post. Appreciated.
This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!
Your article helped me a lot, is there any more related content? Thanks!