From an interviewer's point of view, method overloading and method overriding and the difference between them is an important concept.
Thus, let's understand it.
What is method overloadingthe
Creating more than one method or a function that has a same name but different signatures or parameters in the same class is called method overloading.
Key points
- Method overloading is also called early binding or compile time polymorphism or static binding.
- The compiler automatically calls the required method or the function by checking number of parameters and their type, which are passed into that method.
- If the number of parameters and type doesn't match by any method signatures, then it will give the compile time error.
- We can achive method overloading by changing the number of parameters used or by using different types of parameters or by changing the order of parameters
Example
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace MethodOverloadingDemo
- {
- class Calculator
- {
-
- public int Add(int x, int y)
- {
- int p;
- return p=x + y;
- }
-
- public int Add(int x, int y,int z)
- {
- int q;
- return q = x + y + z;
- }
-
- public float Add(float x, float y)
- {
- float r;
- return r = x + y;
- }
-
-
- public float Add(float x, float y, float z)
- {
- float v;
- return v = x + y + z;
- }
- }
-
-
- class calculatorDemo
- {
- public static void Main(String[] args)
- {
- Calculator calculator = new Calculator();
- calculator.Add(10,20);
- calculator.Add(10,20,30);
- calculator.Add(10f,20f);
- calculator.Add(10f,20f,30f);
-
- Console.ReadLine();
- }
- }
- }
What is method overriding?
Creating a method in the derived class with same signature as a method in the base class is called method overriding
or
Method overriding means having two methods with the same name and same signature, one method in the base class and the other method in the derived class.
Key points
- Method overriding is also called run time polymorphism or dynamic polymorphism or late binding.
- We can override a method in the base class by creating similar function in the derived class. This can be achieved by using inheritance and using virtual & override.
- Same signature means the methods must have the same name, same number of arguments and same type of arguments.
- Method overriding is possible only in the derived classes, but not within the same class.
- When the derived class needs a method with the same signature as in the base class, but wants to execute different code than the one provided by the base class then method overriding will be used.
- Method overriding in C# is a feature like the virtual function in C++.
Important keywords in method overriding
- Virtual keyword
If we use Virtual keyword, then we tell to compiler that this method can be overridden by the derived classes.
- public virtual int Print()
- {
-
- }
- Override keyword
If we use Overrride keyword, then we tell to the compiler that this method is overriding the same named method in the base class.
- public override int Print()
- {
-
- }
- Base keyword
If we use base keyword, then we tell to the compiler that this method calls the base class method for overriding functionality.
Comparison between method overloading and method overriding.
Sr. No | Method Overloading | Method Overriding |
1. | Creating more than one method or function having same name but different signatures or the parameters in the same class is called method overloading. | Creating a method in the derived class with the same signature as a method in the base class is called as method overriding |
2. | It is called the compile time polymorphism | It is called runtime polymorphism |
3. | It has the same method name but with different signatures or the parameters | It must have same method name as well as the signatures or the parameters. |
4. | Method overloading doesn’t need inheritance | Mehod overriding needs inheritance |
5. | Method overloading is possible in single class only | Method overriding needs hierachy level of the classes i.e. one parent class and other child class. |
6. | Access modifier can be any | Access modifier must be public. |
7. | Method overloading is also called early binding. | Method overriding is also called late binding. |