Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Method overloading in Java language

Posted on January 15, 2017February 24, 2020

Table of Contents

  • Method Overloading in Java language
      • 1. Different number of parameter in the list of parameter
      • Example 1
      • 2. Different type of parameter in List of parameter
      • 3. Different order of parameter in List of parameter

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


Method overloading in Java language
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));
}
}

Method overloading in Java language
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));

}

}

Method overloading in Java language
Example

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

Method overloading in Java language
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));
}
}

Method overloading in Java language
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”);

}

}

 

Method overloading in Java language
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”);
}
}

Method overloading in Java language
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

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes