Overloading

  1. When the same name is used for more than one method or constructor, we say that the name is overloaded

  2. Method & constructor names can be overloaded in Java because Java uses

              Method name   +   Parameters' type

    to identify a method.


    Example:
      public void deposit(double amount)   // Deposit only
       {
         balance = balance + amount;
       }
    
      public void deposit(double amount, double cashBack) // with cashback
       {
         balance = balance + amount - cashBack;
       }
    
    Java recognizes that these are 2 different methods by the difference in the parameters' type

    First method has parameters' type (double)
    Second method has parameters' type (double, double)


  3. NOTE: the return value is NOT used to identify a method.

    So... you CANNOT have two methods with identical names and parameters' type but different return values:


    class BankAccount { public double getBalance() { ... } public String getBalance() // Error: Same name & parameters' type { ... } }