Use of Method Overloading in Java
There is always a problem for Java beginners to understand the real-time use of method overloading in Java program or Java project level. They do not understand when to use the method of overloading in Java program?
In this tutorial, we will discuss the real-time use of a method overloading with some practical scenarios.
If you have any difficulty in understanding method overloading, go to this tutorial to learn all the basic topics of method overloading with real-time examples and programs.
Use of Method Overloading in Java Project
Let’s imagine that this class we have created in 2005.
class A
{
public void infoStudent(int age, String loc, int phoneNo)
{
. . . . . .
}
}
Now, clients are calling this class as given below:
Client 1:
// Creating an instance of class A.
A a = new A();
// Calling the inforStudent() method by passing arguments.
a.infoStudent(10, "Dhanbad", 9431765890);
Client 2:
A a = new A();
a1.infoStudent(30, "Ranchi", 9764839020);
Now suppose in 2010, we get a new requirement of capturing Aadhar card of a student. So, what would you do in this case?
To solve this problem, we have two options. They are as follows:
1. First Option:
We can write the code for it like this:
class A
{
// Increasing parameters in the same method.
public void infoStudent(int age, String loc, int phoneNo, int aadharCardNo)
{
. . . . . .
}
}
2. Second Option:
We will create another method with some different name and add the parameters, like this:
class A
{
// Write another method with some different name.
public void aadharCard(int age, String loc, int phoneNo, int aadharCardNo)
{
. . . . .
}
public void infoStudent(int age, String loc, int phoneNo)
{
. . . . .
}
}
Here, our requirement is completed in both cases. Now we will consider clients.
In the first option, we will let the client know about changes in their existing method. The client will say that:
1. We don’t require an aadhar card. why should we change our code?
2. Tomorrow, if any parameters are added, will you be asking us again to change our code?
3. We will need to do lots of testing for changes. We need to convey our some more client to accommodate our change. Please, do not change the methods repeatedly. Write other methods so that if we need them, we will call separately.
In the second option, we will let the client know about changes in their existing method. They will say
1. Ok, we will accommodate, but what changes you have done in your method?
2. Could you explain to us how it will be affecting our code?
3. If everything is the same, the only parameter is increased. So, why you did not give the same name as the method so that we would have not got confused?
4. We will face lots of problems for it.
Tomorrow, if a new developer joins a company, he will not have an idea that both methods are the same because both names are very different.
Now, in such a case, we will use the third option. The third option is using method overloading. It is a feature in object-oriented programming that allows programmers to define multiple methods within the same class, but with different parameter lists. It enables programmers to create cleaner, more concise, and easier-to-maintain code.
class A
{
// Write another method with the same name.
public void infoStudent(int age, String loc, int phoneNo, int aadharCardNo)
{
// For the logic for aadhar card, we have just called the existing method.
infoStudent(int age, String loc, int phoneNo)
}
public void infoStudent(int age, String loc, int phoneNo)
{
. . . .
}
}
How can we communicate with the client?
We have overloaded method infoStudent() for the aadhar card. Now, our clients will automatically understand what has happened?
They will see the effects and will be sure that no logic has been changed overall for an aadhar card only. So, ultimately, the overloading method achieved maintainability and readability of code.
Use of Java Method Overloading in Shopping Cart Project
Let us consider a real-world scenario where we will apply the concept of method overloading in a Java project. Imagine you are working on an e-commerce platform.
You are responsible for designing a shopping cart class that handles various aspects of the user’s shopping experience. Look at the below code how method overloading could be used in an application:
Program code:
public class ShoppingCart {
// Method to add items to the cart
public void addItem(String itemName)
{
// Logic to add a single item to the cart.
System.out.println(itemName + " added to the cart.");
}
// Overloaded method to add items with quantity.
public void addItem(String itemName, int quantity)
{
// Logic to add multiple items with a specific quantity to the cart.
System.out.println(quantity + " " + itemName + "s added to the cart.");
}
// Overloaded method to add items with quantity and price.
public void addItem(String itemName, int quantity, double price)
{
// Logic to add items with quantity and price to the cart.
double totalCost = quantity * price;
System.out.println(quantity + " " + itemName + "s added to the cart. Total cost: $" + totalCost);
}
public static void main(String[] args)
{
// Creating an instance of class shoppingCart.
ShoppingCart cart = new ShoppingCart();
// Calling methods by passing argument values.
cart.addItem("T-shirt");
cart.addItem("Shoes", 2);
cart.addItem("Sunglasses", 3, 25.99);
}
}
Output: T-shirt added to the cart. 2 Shoes added to the cart. 3 Sunglasses added to the cart. Total cost: $77.97
In this example, we have created a ShoppingCart class in which we have done method overloading that provides flexibility when adding items to the cart. This class contains three overloaded versions of the addItem() method, each catering to different user scenarios:
The first version of addItem() method accepts the name of an item and adds it to the cart. The overloaded addItem() method takes the name of an item and a quantity, allowing the user to add multiple items of the same type to the cart.
Another overloaded addItem() method allows the user to specify the item name, quantity, price, calculating the total cost and providing more detailed information.
Inside the main method, we have created an instance of class ShoppingCart and called overloaded methods using it. This example code demos how we can use these overloaded methods to customize the shopping cart experience based on the user’s needs.