articles

Home / DeveloperSection / Articles / Overloading and Overriding in c#

Overloading and Overriding in c#

Overloading and Overriding in c#

Rashmi Sharma889 13-May-2021

Method Overloading :-

1- It is a process of creating a method with the same name and different functionality. In simple words, a method will have same name but its functionality will be different.

2- we can also achieve method overloading in Inheritance. it allows us to create a method in the  super class with same name having different behaviour in super class and sub class.

3- A overloaded method will have different parameters.

When we overload methods in C#?

If you want to perform the same operation but with different types of argument, different types of values.

For example, if you want to add two integers, two floats, and two strings.

   class Calculate

    {

        public void add(int a, int b)

        {

            Console.WriteLine('Sum of int:- '+a + b);

        }

        public void add(float a, float b)

        {

            Console.WriteLine('Sum of float vlaues :- '+a + b);

        }

        public void add(string FirstName, string LastName)

        {

            Console.WriteLine('Name :- '+ FirstName +' '+ LastName);

        }

        static void Main(string[] args)

        {

            Calculate obj = new Calculate();

            obj.add(5, 7);

            obj.add(7.5f, 8.5f);

            obj.add('Rashmi', 'Sharma');

        }

    }

output:-

Sum of int values :- 57

Sum of float values:- 7.58

Name :- Rashmi Sharma

Method Overriding :- 

1- It is a process of creating a method with the same name and same signature in different classes not in same class. In simple words, a method will have same name and parameters will be in different class(super class and sub class).

2- The superclass method is called the overridden method and the sub-class method is called the overriding method.

2- we can achieve method overriding in Inheritance only. it us to create a method in the super class with same name and parameters reimplementing functionality of super class in sub class.

When we override methods in C#?

If the superclass required to be add some implementation then the subclass override that method with the required logic.

How to implement overriding method 

syntax :-

Class1

{

    Public virtual void show(){} //virtual function (overridable)

}

Class2: Class1

{

    Public override void show(){} //overriding

}

For example, if you want to add two integers.

    class Class1

    {

        public virtual void add( int a, int b)

        {

            Console.WriteLine('result of two numbers :- ' + (a+b));

        }

    }

    class Class2 : Class1

    {

        public override void add(int a, int b)

        {

            Console.WriteLine('result of two numbers :- '+ ( a +b)/2);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            Class2 obj2 = new Class2();

            obj2.add(7,9);

        }

    }

Output:-

result of two numbers :- 5

Difference between Method overloading and Method overriding in C#

Method Overloading :-  

1- In this concept multiple methods can be define with the same name but with a different parameters.   

2- Overloading a method can be define within a class or within the child classes also.

3- No keywords are used to implement function overloading.

Method Overriding :-

1- In this concept multiple methods can be define with the same name and same parameters also changes functionality only.

2- Overriding of methods is not possible within the same class it must be define under the child classes.

3- Use the virtual keyword for the base class function and override keyword in the derived class function to implement function overriding.


Updated 14-May-2021
My Hometown is Agra. i have done my schooling from John Milton Public school, Agra. After that my Graduation(B.C.A) specialised in Computer Application was done from Aryan institute of management and computer studies and Post Graduation(M.C.A) specialised in Computer Application from Hindustan Institute of management and Computer Studies. I have started my Career as Programming Trainer in the Computer Training Institute around 2 years from 2015 to 2017 and changed to dot net developer

Leave Comment

Comments

Liked By