05 December 2012

Function overloading

Function overloading in an important concept in OOPS(Object Oriented Programming Concepts). In many technical interviews for Software Developer role, an interviewer will definitely ask questions on function overloading to test the basic understanding of the Object Oriented Programming concepts.

Function overloading is also referred as method overloading.

Straight forward interview questions on function overloading are below

  1. What is function overloading?
  2. What is the significance of function overloading in OOPS and where its useful?
  3. How can we achieve function overloading in .NET?
  4. Will the return type variation in the method declaration also leads to function overloading?
  5. Achieving ploymorphism using function overloading? Will it be compile time polymorphism or runtime polymorphism?

Without much ado, lets start understanding function overloading.

Function overloading means in a class there could be many functions with the same method name but with the varying number of parameters and their data types. i.e. name of the method we will invoke is same, but which implementation of the method is called depends on how many parameters we are passing and what are the data types of the parameters.

Always its better to start with an example.The below program code illustrate function overloading with varying number of parameters in the overloaded methods.

namespace FunctionOverloading
{
    class Program
    {
        static void Main(string[] args)
        {
         // Passing two parameters to Add() method 
         // calls 2 parameters Add() method
         long sum = Calculator.Add(1, 2);
         Console.WriteLine(sum);
         // Passing three parameters to Add() method calls
         // 3 parameters Add() method
         sum = Calculator.Add(1, 2, 3);
         Console.WriteLine(sum);
         // Wait till user enters any character 
         // on the output console screen
         Console.Read();
        }
    }

public static class Calculator
  {
   public static long Add(int operand1, int operand2)
    {
     Console.WriteLine("Two parameter Add() method called");
     long sum = operand1 + operand2;
     return sum;
    }

    public static long Add(int operand1, int operand2, int operand3)
    {
     Console.WriteLine("Three parameter Add() method called");
     long sum = operand1 + operand2 + operand3;
     return sum;
    }
  }
}

Lets have a look at the output to the console.

Two parameter Add() method called
3
Three parameter Add() method called
6

By looking at the above code and the program output, it is clear that depending on the number of parameters passed to the Add() method, a different implementation of the method with the equal number of parameters is executed.

Now let's have a look function overloading with the same number of parameters but with different parameters type.

namespace FunctionOverloading
{
    class Program
    {
        static void Main(string[] args)
        {
          // Passing two parameters to Add() 
          // method calls 2 parameters Add() method
          long sum = Calculator.Add(1, 2);

          Console.WriteLine(sum);

          // Passing int and double calls Add()
          // method with int and double parameters
          sum = Calculator.Add(1, 2.3);

          Console.WriteLine(sum);

          // Wait till user enters any character 
          // on the output console screen
          Console.Read();
        }
    }

    public static class Calculator
    {
        public static long Add(int operand1, int operand2)
        {
            Console.WriteLine("Two parameter Add() method called");
            long sum = operand1 + operand2;
            return sum;
        }
        public static long Add(int operand1, double operand2)
        {
            Console.WriteLine("Two parameters Add() method 
                      with int and double data types called"
);
            long sum = operand1 + (long)operand2;
            return sum;
        }
    }
}

Lets have a look at the output to the console.

Two parameter Add() method called
3
Two parameters Add() method with int and double data types called
3

By looking at the above code and the program output, it is clear that depending on the passed parameter's data type, a different implementation of the method gets executed.

Interesting interview question on function overloading

So far we looked at what is called function overloading. Now, let's discuss about what looks like function overloading but not. (Off-course its an interview question.)

public static void Main(string[] args)
{
  Calculator.Add(1,2);
}

public static class Calculator
{
  public static long Add(int operand1, int operand2)
  {   
      long sum = operand1 + operand2;
      return sum;
  }
  public static int Add(int operand1, int operand2)
  {   
      int sum = operand1 + operand2;
      return sum;
  }
}

Will the above Add() method is overloaded in the Calculator class?

The answer is NO and there will be a compile time error:

Type Calculator' already defines a member called 'Add' with the same parameter types

The call is ambiguous between the following methods or properties: 'Calculator.Add(int, int)' and 'Calculator.Add(int, int)'

Because the binding to call which of the overloaded method to get executed is decided at the compile time and that is based on the parameters number and parameters data types. And hence function overloading is not depend on the return type of the method.

As we discussed using function overloading we can achieve compile time polymorphism. To know more on this read my another post, polymorphism types.

You may also interested in blog post on Function overriding in Object Oriented Programming

Related post covering interesting questions on function overloading