Concept of Overloading and Overriding in c#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 5487 red flag
Rating: 4.5 out of 5  
 2 vote(s)

Concept of overloading and overriding of C#

Concept of Overloading and Overriding in c#

Concept of overloading and overriding is very confusing for beginner. They are very familiar with two terms but don’t know exact difference between them. If you are in same category then this article is for you. Here we will try to understand the concept of overloading and overriding with very simple example.

Before going to discussion we will clear basic concept of them.

Overloading: -  Function name is same but argument set is different and all functions are within same class.

Overriding:- Function name is same function argument is same but functions are in different class. And one class must be derive from other class.

Example of overloading

As per definition, function argument set must be different but function name will be same and functions are in same class. In below example, we have implemented simple overloading concept within Myclass class. Here we are overloading Print() function. If you observe body of Myclass class you will find three different signature of Print() function where function name is same but argument type and number are different. And according to passing argument type and number the appropriate function will invoke.

using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class MyClass
    {
        public void Print()
        {
            Console.WriteLine("sourav Kayal");
        }
        public void Print(String name)
        {
            Console.WriteLine(name + " Kayal");
        }
        public void Print(String name, String surname)
        {
            Console.WriteLine(name + surname);
        }
     }
   
 
    class Program
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.Print();
            obj.Print("Sourav");
            obj.Print("Sourav"," Kayal");
 
            Console.ReadLine();
        }
    }
}

Example of overriding

In case of overriding the function signature will be same but they must be define in different class and one class must be inherited from other class. In below we have implemented sample example to show overriding concept.
using System;
using System.Collections;
using System.Data.SqlClient;
using System.Threading;
using System.Data.Common;
using System.Data;
using System.Reflection;
using System.Globalization;
namespace Test1
{
    class BaseClass
    {
        public virtual void Hello()
        {
            Console.WriteLine("I am base hello");
        }
    }
    class DeriveClass : BaseClass
    {
        public override void Hello()
        {
            //base.Hello();
            Console.WriteLine("I am derive hello function");
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            DeriveClass objD = new DeriveClass();
            objD.Hello();   //Call Hello function of derived class
            Console.ReadLine();
        }
    }
}

In function definition of base class we have to use virtual keyword and in derive class definition we have to use override keyword. 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Posted by: Shitalr on: 8/12/2013 | Points: 25
your article cleared my doubt about overloading and overridding,thanks.
but i have one question what is the use of overriding?where i can use overrindg?

Posted by: t5j9033387989 on: 9/12/2013 | Points: 25
Overloading is defining functions that have similar signatures, yet have different parameters.

Overriding is only pertinent to derived classes, where the parent class has defined a method and the derived class wishes to override that function.

Example

Overriding

public class MyBaseClass

{
public virtual void MyMethod()
{
Console.Write("My BaseClass Method");

}
}
public class MyDerivedClass : MyBaseClass
{
public override void MyMethod()
{
Console.Write("My DerivedClass Method");

}
}


Overloading

int add(int a, int b)

int add(float a , float b)

Posted by: t5j9033387989 on: 9/12/2013 | Points: 25
this isa nice article thanks bro. i put hereone more example to understand it clearly hope this will be helpful..
Posted by: Longwest on: 7/21/2015 | Points: 25
Thank you, Sourav
This article is really excellent and of help to me.

Best regards.
http://www.keepdynamic.com/

Login to post response

Comment using Facebook(Author doesn't get notification)