Overview

Java is a statically typed, compiled, and general-purpose programing language. The signature of a function in Java is the combination of its name and parameters. No two functions defined within a common class can have the same signature. In this article, we shall learn how to use the signature of a method in Java to define multiple methods with the same names.

Scope

This article is all about:

  • Requirement Of Method Signature In Java.
  • How To Modify The Signature Of A Java Method:
  • Modifying the name.
  • Changing the sequence of parameters.

This article however is not about:

  • Any other unrelated Java concepts.

Why Do We Need Java Method Signature

Have a look at the code block below as well as its output

public class Main{
  public int sum(int a, int b){
      return a+b;
  }
  public double sum(int a, int b){
      return (double)(a+b);
  }
  public static void main(String args[]){
      Main item= new Main();
      System.out.println(item.sum(56, 34));
  }
}

Output

/Main.java:5: error: method sum(int,int) is already defined in class Main
  public double sum(int a, int b){
                ^
1 error

write your code here: Coding Playground

The signature, i.e., the combination of the name and parameter sequence of both the “sum” functions of the Main class is the same. This is not allowed in Java. But by modifying the signature of our sum function(s) we can define multiple sum functions within the same (Main) class!

How To Change The Signature Of A Java Method

Change the name of function(s)

As seen in the previous example, we need multiple function definitions with the same name and sequence of parameter da-types. We can assign names to all these functions such that the naming is still relevant to the purposes that these functions serve. Example:

public class Main{
  public int sum(int a, int b){
      return a+b;
  }
  public double sumDouble(int a, int b){
      return (double)(a+b);
  }

  public String sumString(int a, int b){
      return ""+(a+b);
  }
  public static void main(String args[]){
      Main item= new Main();
      System.out.println(item.sumDouble(56, 34));
  }
}

Output

90.0

write your code here: Coding Playground

Chaing the parameter sequence

Parameter sequence means the order of the data types of the function arguments. If the number of parameters in two function definitions differ, changing the parameter sequence is not required. The code will work just fine. Even if the number of parameters in two function definitions is the same, so long as there is a minimum of one position at which the parameters are of different types in the two functions, there should not be a problem.

But what if the parameter sequence of two functions is of the same size and the same order of data types? A compilation error is thrown, as we observed in the very first example. The following example dictates how we can change the order of parameters based on the sequence of their data types, to make the code compilation error free:

public class Main{
  public void print(int a, String b, Boolean c){
      System.out.println(a+" | "+b+" | "+c);
  }
  public void print(String a, int b, Boolean c){
      System.out.println(b+" | "+a+" | "+c);
  }
  public void print(Boolean c, int a, String b){
      System.out.println(c+" | "+b+" | "+a);
  }
  public static void main(String args[]){
      Main item= new Main();
      item.print("alpha", 45, false);
      item.print(45, "alpha", false);
      item.print(false, 23, "alpha");
  }
}

Output

45 | alpha | false
45 | alpha | false
false | alpha | 23

write your code here: Coding Playground