blog

Home / DeveloperSection / Blogs / Method Overloading

Method Overloading

Jayden Bell1648 16-May-2016

In our example of the previous post, the classes Asset, BankAccount, and SavingsAccount defined two constructors. Both constructors obviously have the same name. They simply differ in the number of arguments they accept. This feature is called method overloading, where two methods defined in a class have the same name. They must, however, differ from each other in some aspect so that the compiler knows which version to use for binding. Before discussing these aspects and covering the rules of method overloading, let’s add another constructor to our Savings Account class to see the purpose behind method overloading.                

Asset

-id: int

-type: string

+Asset( )

+Assset( in id: int, in type: string)

+printDescription(): void

 

BankAccount

-bankName: string

-balance: float

+BankAccount( )

+BankAccount(in bankName: string, in balance: float, in id: int, in type: string)

+printDescription(): void

 

SavingAccount

-interestRate: float

+SavingvAccount( )

+SavingvAccount(in interestRate: float, in bankName: string, in balance: float, in id: int,

  in type:string )

+printDescription(): void


In our example, we defined two constructors for the SavingsAccount class—one with no arguments and the other one with several arguments. The one with no arguments initialized the savings account object with null strings and zeroes, which does not make sense in real life. The other constructor required lots of arguments, so if we are constructing several objects, we will need to type in all these arguments in each call to the constructor. When we create a bank account, typically the bank name and the account number are sufficient information to be captured from the user. The rest of the values (the object’s state) may be generated internally in the program.

Therefore, it would be sufficient to have a constructor that takes only two parameters. The definition of such a constructor is given in the following code snippet:

public SavingsAccount (String bankName, int accountNumber) { 

this (5.0f, bankName, accountNumber, 0, 10001, "Bank Account");

}

 

As before, this definition indicates a constructor declaration because the method name is the same as the class name and the method does not return anything. The constructor takes only two arguments—the ones we want. In the body of the constructor, the one and only statement is a call to this. Earlier we used this keyword to refer to the fields of the current class. In this case, the this keyword is used to call a constructor belonging to the same class. Thus, we put opening and closing parentheses after this call with the required number of parameters embedded within.

The call to this contains six parameters, which results in calling the constructor that takes six parameters. The two parameters are supplied by the caller of the this constructor. The rest of the parameters are hard-coded and could be generated internally in the SavingsAccount class. Now,to call this constructor, the developer uses the following:

SavingsAccount anitaSavingsAccount = new SavingsAccount("HSBC", 1022);
System.out.println("Anita's Savings Account");
anitaSavingsAccount.printDescription();
System.out.println(lineSeparator);

Output:

Add this code to the earlier main method and the previously defined two-argument constructor to the SavingsAccount class. Run the application; we will get the details of Eric’s savings account printed to the console, as follows:

-------------------

Eric's Savings Account

A savings account

Asset ID: 10001

Asset type: Bank Account

Name: HSBC

Account #: 1022

Current balance: $0.0

Interest rate (%): 5.0

-------------------

 Note how the savings account object is properly initialized with the desired values. We can easily see that this simplifies our object-creation process because we need to supply only a few parameters. It’s very common for a constructor with fewer parameters to call a constructor with more parameters, supplying default values. For this usage, the constructors with fewer parameters will frequently consist of only the this call. The compiler matches this with a constructor with the appropriate number and types of parameters and then calls it.

Also, a call to the this constructor must be the first statement in the implementation of the constructor. It cannot appear anywhere else. Therefore, we can have only one call to this, which must be the first statement in the method body. If we call this, we cannot call super because super cannot appear anywhere else other than the first statement.

Note that method overloading need not be restricted to compiler declarations. It can be applied to any other method of the class. For example, we may want to create another variation of the printDescription method that takes a few parameters. In that case, we overload the printDescription method.

Rules of Method Overloading

The overloaded method must differ from the existing method in the number of parameters and/or the order of parameters. In the case of two methods where the number of parameters is the same and they are all of the same type in each of the methods, rearranging the parameters into a different order results in an overloaded method. While differentiating between the two overloaded methods, the compiler ignores their return types and the exceptions thrown. Therefore, if the two overloaded methods only differ in their return types or exceptions thrown, we will get a compilation error.


Updated 14-Mar-2018

Leave Comment

Comments

Liked By