Method Overloading and Method Overriding in C# with example

Method Overloading and Method Overriding in C# with example

This article explains what is Method overloading and Method overriding in C# with example and difference between them. This is a basic question asked by many interviewers.

Method Overloading (Compile Time Polymorphism/ Static Polymorphism/Early Binding)

Method Overloading means more than one method having same name but different Signatures (Parameters) in the same class or different class.

Example

public void Add(int a, int b)

        {

            Console.WriteLine(a + b);

        }

public void Add(int a, int b, int c)

        {

            Console.WriteLine(a + b + c);

        }

In above class we have two methods with same name but having different input parameters this is called method overloading or compile time polymorphism or early binding.

Method Overriding (Run Time Polymorphism/ Dynamic Polymorphism/Late Binding)

Method overriding means having two methods with same name and same signature, one method in base class and other method in derived class.
we can override a method in base class by creating similar function in derived class this can be achieved by using inheritance and using “virtual & override” keywords.

We have to use the virtual keyword for the method which in base class and override keyword for the method in derived class.

Example

namespace ConsoleApplication1

{

    class Shape

    {

        //Method to be overridden in derive class

        public virtual void Draw()

        {

        }

    }

    class Ractangel : Shape

    {

        public override void Draw()

        {

            Console.WriteLine("Rectangle Drawn ");

        }

    }

    class Circle : Shape

    {

        public override void Draw()

        {

            Console.WriteLine("Circle Drawn ");

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Shape[] s = new Shape[2];

            /* creating Array with Different types of Objects */

            s[0] = new Circle();

            s[1] = new Ractangel();

            Console.WriteLine("\n\nRuntime polymorphism test\n\n");

            for (int i = 0; i < 2; i++)

            {

                s[i].Draw();

            }

 

            Console.ReadKey();

        }

    }

}

Result



Difference between method overloading and method overriding

Overloading

Overriding

Having same method name with different Signatures (parameters).

Methods name and signatures must be same.

Overloading is the concept of compile time polymorphism.

Overriding is the concept of run time polymorphism.

Two functions having same name and return type, but with different type and/or number of arguments is called as Overloading.

When a function of base class is re-defined in the derived class called as Overriding.

 

It doesn't need inheritance.

It needs inheritance.

Method can be different access modifiers.

Method should be public.

 

Summary

I hope this article will help you to understand the concept of Method overloading and Method overriding in C#. Please share your valuable feedback on this.

comments powered by Disqus

Related Articles