C#

Difference Between Method Overloading And Method Overriding


What is method overloading

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.


1. Method overloading is also called early binding or compile time polymorphism or static binding.
2. The compiler automatically calls the required method or the function by checking number of parameters and their type, which are passed into that method.
3.  If the number of parameters and type doesn't match by any method signatures, then it will give the compile time error.
4. We can achieve 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;

namespace Test
{
    class Calculate
    {
        public int ShowSum(int x, int y)
        {
            return x + y;
        }

        public float ShowSum(float x, float y)
        {
            return x + y;
        }
        public float ShowSum(float x, float y,float z)
        {
            return x + y+z;
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            Calculate cal = new Calculate();
            Console.WriteLine(cal.ShowSum(2,5));
            Console.WriteLine(cal.ShowSum(2.5f, 5.7f));
            Console.WriteLine(cal.ShowSum(2.5f, 5.7f,8.9f));
            Console.ReadKey();
        }
    }
}

overloading overriding

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

Key points

1. Method overriding is also called run time polymorphism or dynamic polymorphism or late binding.
2. 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.
3. Same signature means the methods must have the same name, same number of arguments and same type of arguments.
4. Method overriding is possible only in the derived classes, but not within the same class.
5. 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.
Important keywords in method overriding

1.  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()
        {
            return 0;
        }

2. Override keyword

If we use Override keyword, then we tell to the compiler that this method is overriding the same named method in the base class.
public override int Print()
        {
            return 10;
        }

 Comparison between method overloading and method overriding.


overloading 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



No comments:

Post a Comment