Define Multiple Methods with the Same Name in Java



Yes, we can define multiple methods in a class with the same name. But if we have two methods with the same name, the compiler should be able to differentiate between the two methods. 

Therefore, in Java, "we can define multiple methods" with the same name in a single class, as long as each method has a different set of parameters. When we invoke a method, the compiler executes the respective body (code) based on the arguments passed.

This concept is known as method overloading.

Method Overloading in Java

In Java, method overloading is a type of compile-time polymorphism. Polymorphism is one of the core concepts of OOP (Object-Oriented Programming), which means having one name but many forms.

In method overloading, multiple methods can have the same name but different parameter lists (different numbers or types of parameters).

Syntax of Method Overloading (Compile-time Polymorphism)

Following is the syntax for defining method overloading in Java:

public class className{
   public method_name(data_type para1){}
   public method_name(data_type para1, data_type para2) {}
   public method_name(data_type para1, data_type para2, data_type para3) {}
}

Example 1: Same Method with Different Types of Parameters

In the following example, we define three methods having the same name, displayDetails(), but with different types of parameters, such as int, float, and string:

public class MethodOverloadingTest {

   //three methods having same with different types of parameters
   public static void displayDetails(String name){
      System.out.println("Name: " + name);
   }
   
   public static void displayDetails(int age){
      System.out.println("Age: " + age);
   }
   
   public static void displayDetails(float marks){
      System.out.println("Marks: " + marks);
   }
   
   //main method
   public static void main(String[] args) {
      displayDetails("John");
      displayDetails(20);
      displayDetails(89.54f);
   }
}

The above program produces the following output:

Name: John
Age: 20
Marks: 89.54

Example 2: Same Method with Different Numbers of Parameters

This is another example of defining multiple methods in a single class with the same name. We define two methods named addSum(), both having the "same type of parameters", but a "different number of parameters":

public class MethodOverloadingTest {
   
   public static int addSum(int a, int b){
      return a+b;
   }
   
   public static int addSum(int a, int b, int c){
      return a+b+c;
   }
   
   //main method
   public static void main(String[] args) {
      int a = 10;
      int b = 20;
      int c = 30;
      System.out.println("Sum of " + a + " + " + b + " is: " + addSum(a, b));
      System.out.println("Sum of " + a + " + " + b + " + " + c + " is: " + addSum(a, b, c));
   }
}

Following is the output of the above program:

Sum of 10 + 20 is: 30
Sum of 10 + 20 + 30 is: 60
Updated on: 2025-05-14T13:11:32+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements