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).

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s