Method Overloading :
Method Overloading is a feature that allows a class to have two or more method in the same class with same name but their parameters declarations are different (different number of parameters, different types of parameters, or both).
It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.
There are different ways to perform method overloading :
- By changing the number of parameters
- By changing the datatypes of the parameters
- By changing the sequence of datatype of parameters.
1.By changing the number of parameters :
package com.tutorial4u;
class Addition {
public void sum(int a, int b) {
int sum = a+b;
System.out.println("sum of a and b :"+sum);
}
public void sum(int a , int b, int c) {
int sum = a+b+c;
System.out.println("sum of a, b, and c : "+sum);
}
}
public class OverloadingExample {
public static void main(String[] args) {
Addition add = new Addition();
add.sum(10, 20);
add.sum(10, 20, 30);
}
}
Output :
sum of a and b :30
sum of a, b, and c : 60
2. By changing the datatypes of the parameters :
package com.tutorial4u;
class Addition {
public void sum(int a, int b) {
int sum = a+b;
System.out.println("sum of a and b :"+sum);
}
public void sum(double a , double b) {
double sum = a+b;
System.out.println("sum of a, b, and c : "+sum);
}
}
public class OverloadingExample {
public static void main(String[] args) {
Addition add = new Addition();
add.sum(10, 20);
add.sum(10.5, 20.5);
}
}
Output :
sum of a and b :30
sum of a, b, and c :
31.0
3.By changing the sequence of datatype of parameters.
package com.tutorial4u;
class Tutorial {
public void display(int a, char c) {
System.out.println("I am the first definition of method");
}
public void display(char c , int b) {
System.out.println("I am the second definition of method");
}
}
public class OverloadingExample {
public static void main(String[] args) {
Tutorial add = new Tutorial();
add.display(10, 'a');
add.display('b', 20);
}
}
Output :
I am the first definition of method
I am the second definition of method
Case 1: what happen when method signature is same but return type is different ?
package com.tutorial4u;
class Addition {
public int sum(int a, int b) {
int sum = a+b;
return sum;
}
public double sum(int a , int b) {
double sum = a+b;
return sum;
}
}
public class OverloadingExample {
public static void main(String[] args) {
Addition add = new Addition();
add.sum(10, 20);
add.sum(10, 20);
}
}
Answer : It will through a compilation error. like : Duplicate method sum(int, int) in type Addition
Case 2: Can we overload java main() method?
Answer : Yes, We can overload the main method any numbers of times but JVM calls main() method which receives string array as arguments only .
Example :
class OverloadingExample{
public static void main(String[] args){
System.out.println("method 1");
}
public static void main(String args){
System.out.println("method2");
}
public static void main(){
System.out.println("method3");
}
}
Output : method 1
Important Points :
Important Points :
- Java does not allow overloading by changing the return type, though overloaded methods can change the return type.
- private methods can be overloaded.
- final methods can be overloaded.
- Main method can be also overloaded.
- static method can be overloaded.