What is method overloading?

 Posted by Raja on 4/5/2009 | Category: OOPS Interview questions | Views: 16901

Method overloading allows us to write different version of the same method in a class or derived class. Compiler automatically select the most appropriate method based on the parameter supplied.

public class MultiplyNumbers

public int Multiply(int a, int b)
return a * b;

public int Multiply(int a, int b, int c)
return a*b*c;

To call the above method, you can use following code.
MultiplyNumbers mn = new MultiplyNumbers();

int number = mn.Multiply(2, 3) // result = 6
int number1 = mn.Multiply(2, 3, 4) // result = 24

You can't have a overload method with same number parameters but different return type. In order to create overload method, the return type must be the same and parameter type must be different or different in numbers.

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response