go to previous page   go to home page   go to next page

A class has the following two methods:

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

Answer:

No.


Return Type is Not Part of the Signature

The names of the formal parameters are not part of the signature, nor is the return type. The signatures of the two methods are:

chargePenalty( int )
chargePenalty( int )

It might seem strange that the return type is not part of the signature. But a potentially confusing situation is avoided by keeping the return type out of the signature. Say that there were two methods that differ only in their return type:

float chargePenalty( int amount  ) { ... }
int   chargePenalty( int penalty ) { ... }

and that main used one of the methods:

class CheckingAccountTester
{
  public static void main( String[] args )
  {
    CheckingAccount bobsAccount = new CheckingAccount( "999", "Bob", 100 );
    
    double result = bobsAccount.chargePenalty( 60 );

  }
}

Which method should be called?

Either method matches the statement, since both have and int parameter and both return a type that can be converted to double. (Both int and float can be converted to double.)

To avoid confusion in situations like this, the return type is not counted as part of the signature.


QUESTION 14:

Say that a class has these two methods:

public void changeInterestRate( double newRate ) { ... }
public void changeInterestRate( int    newRate ) { ... }

Do these methods have unique siqnatures?