Method overloading is just overwriting an existing method with the same method name but varying in parameters part.

Imagine you need to perform addition for two numbers as well as three numbers. The addition method is same but number of values or parameters to be added in both cases are only different. Right?

We should create methods for addition of two numbers and three numbers both separately and the name of the methods should be different. Since both the methods are addition but the names of methods are different, the readability of the program will be reduced. To overcome similar situations, we can define many methods with same name differentiating the methods in parameters part.

• By varying in number of parameters.
• By varying in datatype of the parameters.

## By varying in number of parameters

### Example

int sum=a+b;

System.out.println("Sum of two numbers is "+sum);

}

int sum=a+b+c;

System.out.println("Sum of three numbers is "+sum);

}

### Full program

//addition method of parameter a and b

int sum=a+b;

System.out.println("Sum of two numbers is "+sum);

}

//addition method of parameter a, b and c

int sum=a+b+c;

System.out.println("Sum of three numbers is "+sum);

}

public static void main(String args[]){

}

}

Try In Editor

### Output

Sum of three numbers is 17

Sum of two numbers is 25

## By varying in datatype of parameters

### Example 1

void display(int num){

System.out.println("Displaying the number "+num);

}

void display(char c){

System.out.println("Displaying the character "+c);

}

### Full program

//display method of parameter num of int datatype

void display(int num){

System.out.println("Displaying the number "+num);

}

//display method of parameter num of char datatype

void display(char c){

System.out.println("Displaying the character "+c);

}

public static void main(String args[]){

ovl.display(1);

ovl.display('d');

}

}

Try In Editor

### Output

Displaying the number 1

Displaying the character d

### Example 2

int sum=a+b;

System.out.println("Sum of two int values is "+sum);

}

float sum=a+b;

System.out.println("Sum of int and float value is "+sum);

}

### Full program

//addition method of two int parameters a and b

int sum=a+b;

System.out.println("Sum of two int values is "+sum);

}

//addition method of int parameter a and float b

float sum=a+b;

System.out.println("Sum of a int and float value is "+sum);

}

public static void main(String args[]){