Dot Net For All

C# Method Overloading and Overriding With Examples

Method Overloading and Overriding in C#

In this article I want to discuss the two important concepts of OOP. The concept is known as polymorphism. As we can know that polymorphism is to have different traits(features) of the same object. The polymorphism in OOP is achieved by two ways and they are known as compile time polymorphism and run time polymorphism. Both of these are achieved by function overloading and function overriding in C# respectively. Here I will discuss both of these one by one and compare the differences with examples

  1. Compile Time Polymorphism(Function Overloading)

    This is the type of polymorphism in which the single class defines two or more versions of a same function. The object of that class is used to determine at the compile time itself that which function do we need to call to achieve a given functionality for that instance.Below is an example of function overloading.

        public class AddClass
        {
            public void Add(int x, int y)
            {
            }
    
            public void Add(int x, int y, int z)
            {
            }
        } 
    

    At compile time itself we can decide which function to call as shown in the following figure

    As we can see that while calling the function using the instance of the class we will get the option of choosing the function using intellisense in Visual Studio.

    However we cannot overload the methods just on the basis of the return type of the methods as shown below. We will get the compile time error.

    However we can overload the methods even if we have same number of parameter but type of the parameter are different as shown below.

            public int Add(int x, float y) { return x + Convert.ToInt16(y); }
            public float Add(int x, int y) { return (float)(x + y); }

    We cannot overload the methods based on the params modifier for the parameter. The following is invalid scenario

            public int Add(int x, int[] y) { return x + y[0]; }
            public int Add(int x, params int[] y) { return (x + y[0]); }

    The functions also cannot be overloaded just by the ref and out keywords. The reason you can find here in one of my article.

  2. Run time polymorphism(Function Overriding) –

    Run time polymorphism can be achieved by overriding the functions of the base class in the derived class . In this case the compiler decides at the run time which functions to call based on the instance variable type at run time.This polymorphism can be achieved by using the virtual, abstract and override keywords discussed here in one of my article.Function overriding can be achieved by using the principle of inheritance.
    Have a look at the following code for better understanding.

       public class BaseClass
        {
            public virtual void MyFunction()
            {
                Console.WriteLine("In Base Classs");
            }
        }
    
        public class DerivedClass:BaseClass
        {
            public override void MyFunction()
            {
                Console.WriteLine("In DErived Classs");
            }
        }
    

    In the above code sample I have defined two classes in which DerivedClass is inherited from the BaseClass and function MyFunction() is overridden in the derived class.
    Now if I execute the below code

                BaseClass baseIns = new BaseClass();
                baseIns.Function();
    
                BaseClass derivedIns = new DerivedClass();
                derivedIns.Function();
    

    In the above code in first case I have created an instance of BaseClass and in second case I have created an instance of DerivedClass.If we check the output of the above code we will get the following result.

    In the above case the compiler decides at run time which function to call.
    In one of my article you can learn how the compiler decides about which function to call at run time in case of virtual functions.

In this article I have discussed two important concepts of object oriented programming that is Compile time polymorphism and run time polymorphism and how we can use them effectively to achieve the desired goals.

 

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview