Method Signature

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

  1. statement A calls processDeposit(int)
  2. statement B calls processDeposit(int, int)

Method Signature

When several methods have the same name, only one is picked by a method call:

The types of the actual parameters in a method call are matched with the types of the formal parameters of the methods. If an exact match cannot be made, then the actual parameters are converted to types that match the formal parameters if this can be done without potential loss of information.

For example, the call

bobsAccount.processDeposit( 200, 25 );  //statement A

matches this method declaration:

public void  processDeposit( int amount, int serviceCharge )

because the number and types of the actual parameters matches the number and types of the formal parameters.

The signature of a method is:

  • Its name.
  • The number and types of its parameters, in order.

The signatures of the methods in a class must be unique. For example, the signatures of the two processDeposit methods are:

  • processDeposit( int )
  • processDeposit( int, int )

The names of the parameters are not part of the signature because parameter names are not visible outside of their scope.

The return type is not part of the signature. The visibility modifier is not part of the signature.

QUESTION 13:

Say that a class has the following two methods:

float chargePenalty( int amount  ) { ... }
int   chargePenalty( int penalty ) { ... }
Do these methods have unique signatures?