Table of Contents
Method Overloading in Java language
In this tutorial, we will discuss the concept of method overloading in Java language.
- Number of parameter
- Datatype of parameter
- Order of datatype in parameter
1. Different number of parameter in the list of parameter
Example 1
class overloding1{
public static void main(String args[]){
add(34,45,65);
//calling method 1 and passing argument for int
add(34,45);
//calling method 2 and passing argument for int
}
public static void add(int a, int b, int c){
//method1 passing 3 parametter for int
System.out.println(“Answer of this method 1: “+(a+b+c));
}
public static void add(int a, int b){
//method1 passing 2 parametter for int
System.out.println(“Answer of this method 2: “+(a+b));
}
}
Example 2
class methodoverlaod{
public static void main(String args[]){
Example obj=new Example();
obj.add(56,87,65);
//calling method 1 and passing argument for int
obj.add(56,87);
//calling method 2 and passing argument for int
}
}
class Example{
public static void add(int a, int b, int c){
//method1 passing 3 parametters for int
System.out.println(“Answer of this method 1: “+(a+b+c));
}
public static void add(int a, int b){
//method1 passing 2 parametters for int
System.out.println(“Answer of this method 2: “+(a+b));
}
}
In the two programs above, the same method name and same data type are used but with the different number of parameters.
2. Different type of parameter in List of parameter
Example 1
class overloading2{
public static void main(String args[]){
add(34,45,65);
//calling method 1 and passing argument for int
add(47.0,45.0,55.0);
//calling method 2 and passing argument for double
add(“sunthan”,” “,”mathy”);
//calling method 3 and passing argument for String
}
public static void add(int a, int b, int c){
//method1 passing parametter int
System.out.println(“Answer of this method 1: “+(a+b+c));
}
public static void add(double a, double b, double c){
//method2 passing parametter double
System.out.println(“Answer of this method 1: “+(a+b+c));
}
public static void add(String fname,String space, String lname){
//method3 passing parametter String
System.out.println(“Answer of this method 1: “+(fname+space+lname));
}
}
When the above code is executed, it produces the following result
Example 2
class methodoverlaod1{
public static void main(String args[]){
Example obj=new Example();
obj.add(56,87,65);
//calling method 1 and passing argument for int
obj.add(56.56,87.01,90.87);
//calling method 2 and passing argument for int
}
}
class Example{
public static void add(int a, int b, int c){
//method1 passing 3 parametters for int
System.out.println(“Answer of this method 1: “+(a+b+c));
}
public static void add(double a, double b,double c){
//method1 passing 2 parametters for int
System.out.println(“Answer of this method 2: “+(a+b+c));
}
}
In the program above, the same method name and the same number of the data type are used but with different data type of parameter.
3. Different order of parameter in List of parameter
When methods have the same name, the same type of parameter but a different order of parameter in the parameter list.
class overloading3{
public static void main(String args[]){
add(45,’x’);
//calling method 1 and passing argument for int and char
add(‘x’,45);
//calling method 2 and passing argument for char and int
}
public static void add(int a, char c){
System.out.println(“The is first method of overloading”);
}
public static void add(char c, int a){
System.out.println(“The is second method of overloading”);
}
}
class methodoverlaod1{
public static void main(String args[]){
Example obj=new Example();
obj.add(56,87.76);
//calling method 1 and passing argument for int
obj.add(56.56,87);
//calling method 2 and passing argument for int
}
}
class Example{
public static void add(int a, double b){
//method1 passing 2 parametters for int and double
System.out.println(“display of this method 1”);
}
public static void add(double b, int a){
//method1 passing 2 parametters for double and int
System.out.println(“display of this method 2”);
}
}
In the program above, the same method name and the same number of the data type are used but with different order of data type of parameter.
Related Links
Constructor in Java Method overriding in Python
Method in Java
Abstraction in C++ Abstraction in Java
Encapsulation in C++ Encapsulation in Java
Polymorphism in C++ Methodoverriding in Java
Method overloading in Java Constructor overloading in Java