Method overloading in Java language

Method Overloading in Java language

In this tutorial, we will discuss the concept of method overloading in Java language.

Method overloading is one of the features of Java which helps the methods with the same name. Java may contain many methods with the same name but with different parameter list.
What are the different of parameter list?
  • Number of parameter
  • Datatype of parameter
  • Order of datatype in parameter

1. Different number of parameter in the list of parameter

When two methods have the same name but number of parameters are different in parameter list

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


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));
}
}

Example

 

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

When methods have the same name but types of the parameter are different in the parameter list,

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));

}

}

Example

When the above code is executed, it produces the following result

Output

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));
}
}

Example

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”);

}

}

 

Example

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”);
}
}

Example

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

Exception Handling in Java

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.