Method Signature in Java
Method Signature in java is defined as the structure of a method that is designed by the programmer. Method Signature is the combination of a method's name and its parameter list. A class cannot have two methods with the same signature. If we declare two methods with the same signature, compilation error is thrown.
Method signature does not include the return type of a method.
Why do we need a Method Signature in Java?
Let's suppose you want to declare two methods of the same name for example getArea, the first method is used to find the area of a rectangle, and another method of the same name is used to find the area of a square.
How can you define two methods of the same name? The answer is to use different method signatures of both methods. We can declare two methods of same name by creating a difference in the parameter list of both methods. Two parameter lists are different if either of the following conditions are satisfied:
- Different number of parameters in both parameter lists
- At least one parameter is of different type
This event of declaring two methods with same name but different parameter lists (or signature) is called Method Overloading.
An example of Method Overloading is provided below. You can notice, there are two methods with same name sum but different number of parameters.
Let's understand how to implement method signature in java.
Example of Method Overloading
In the example below, we are creating two methods of the same name i.e getArea with method signatures. The first method is used to calculate the area of a rectangle, and the second method is used to calculate the area of a square.
Output:
Example 1: Java Method Signature
In Java, we can have four forms of method signatures. Let's understand each of them one by one.
-
Without return type and no parameters.
In this form, the method doesn't have its return type i.e the return type is void and without a parameter.
The printName() method doesn't have any return type and parameter in the below example.
Output:
-
Without Return Type and with Parameter.
In this form, the method doesn't have its return type i.e the return type is void but contains parameters.
The printName() method doesn't have any return type but contains a parameter of String type.
Output:
-
With return type and no parameter
In this form, the method has its return type but without any parameters.
Output:
-
With return type and with Parameter.
In this form, the method has both return type and parameters.
The printName() method has a return type of String, and also contains a parameter of String type.
Output:
Example 2: Class with Two Methods with the Same Signature
In java, we cannot implement two methods of the same signature i.e of the same method name and same parameter list. Because it will create a confusion at run time regarding which method to call. Even if the return types are different, two methods with same signatures will throw compile-time error.
Let's understand this using an example.
In the below program we are implementing two methods of the same signature. Let's see what will happen when we try to execute our program.
Output:
Learn More
We can learn more about Java Methods and their characteristics through this article: Methods in Java
Conclusion
- Method Signature of a method is composed of the method name and its parameter list.
- It is not possible to declare two methods in a class with the same method signature.
- Method signature does not include return type of a method.
- Declaring two methods with same name but different signatures is called Method Overloading.
- We cannot achieve Method Overloading by changing the return type of two methods.