Tag Archives: Method Signature

Java: Method Overloading


Java method overloading allows different methods with the same name, but different signatures. Where the signature can be different by the number of input parameters or type of passing parameters or both.

See Also:

Advantage of method Overloading

  • Don’t need to remember so many method names.

Points to remember about Method Overloading

  1. Method Overloading is related to the concept of compile-time (or static) polymorphism.
  2. Method Overloading possible on the same class.
  3. Method Overloading doesn’t consider the return type of method. If two method signature is the same and only differ in return type then the compiler will show as a duplicate method.
  4. Method Overloading can overload static methods but not based on keyword static only.
  5. Method Overloading can also be done for the main() method.
  6. Java language doesn’t support user-defined operator loading but internally supports operator overloading for example : (+) for concatenation.

Note: (Java 8+)If both methods have the same parameter types, but different return type, then it is not possible.

Ways to do Method Overloading

Method overloading is possible by:

  • Change the number of parameters in methods.
  • Change data types of the parameters in methods.
  • Change Order of the parameters of methods.

How to determine parameter matching in Overloading with different Types?

Parameter matching determines when parameter type is different but to higher type(in terms of range) in the same family.

For Example: suppose passing argument type is int, then check for an overloaded method of type int if not found then check for the data type of higher in the same family( long type if still not found then check for the float data type).

public class Demo {
	//overloaded methods
	public void show(int x) {
		System.out.println("In int " + x);
	}

	public void show(String s) {
		System.out.println("In String " + s);
	}

	public void show(byte b) {
		System.out.println("In byte " + b);
	}

	public static void main(String[] args) {
		byte a = 25;
		Demo obj = new Demo();
		// If will call method with by argument
		obj.show(a);
		obj.show("Facing Issues On IT "); // String
		obj.show(250); // Int
		/*
		 * Since method with char type is not available,
		 * so the data type higher
		 * than char in same family is int
		 */
		obj.show('A');
		/*
		 * Since method with float data type is
		 * not available and so it's higher
		 * data type, so at this step their will
		 * be an compile time error.
		 */
		// obj.show(7.5);
	}
}

Output


In byte 25
In String Facing Issues On IT 
In int 250
In int 65

 

Java: Method Signature


A method signature is part of the method declaration where a signature is a combination of the method name and parameter list. Method signature doesn’t consider return type as part of the signature.

The main reason to emphasis on method Signature is because of method overloading. Java compiler distinguishes between the methods based on the method signature.

See Also:

Method Overloading :
Method overloading provides the ability to write methods with the same name but accept different arguments. Method Overloading is compile-time Polymorphism.

Here is one example of method overloading where add method implements by three ways:

  1. double add(double a, double b)
  2. int add(int a, int b)
  3. double add(int a, int b)

Method signature doesn’t consider return type for method overloading, it will consider method 2 and 3 as a duplicate method and return this compile-time error.

Duplicate method add(int, int) in type MethodSignatureExample

As shown in below screen shot.

Java: Method Signature Example
Java: Method Signature Example

Method Signature Examples

public void getMapLocation(int xPos, int yPos)
{
//method code
}

The method signature in the above example is getMapLocation(int, int). In other words, it’s the method name (getMapLocation) and the parameter list of two integers.

public void getMapLocation(Point position)
{
//method code
}

The Java compiler will allow adding another method because its method signature is different, getMapLocation(Point).