What is method overloading?

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s