Java

How to Perform Overloading in Java4 min read

same name but different parameter lists in the same class. When you call one of these methods, the Java runtime determines which method to execute based on the number and types of arguments you provide.

Here is an example of method overloading in Java:




In this example, the “Calculator” class defines three methods called “add”, each with a different parameter list. You can call these methods like this:

Overloading 

In programming, overloading refers to the ability to create multiple methods or functions with the same name but different parameter lists. This allows you to create more flexible and reusable code, as you can use the same method or function to perform different tasks depending on the input provided.

For example, you might create an “add” method or function that takes two integers and returns their sum. You could then create additional versions of this method or function that take different types of arguments, such as three integers, two floats, or an integer and a float.

When you call one of these overloaded methods or functions, the runtime or interpreter determines which version to execute based on the number and types of arguments you provide. This process is known as method or function overloading.

Overloading is a common feature in many programming languages, including Java, C++, and Python. However, the specific syntax and rules for overloading may vary between languages.

Why overloading?

There are several benefits to using overloading in programming:

  1. Code reuse: By creating multiple versions of a method or function with the same name, you can avoid repeating code and make your program more modular and easier to maintain.
  2. Flexibility: Overloading allows you to create methods or functions that can be used in a variety of contexts, depending on the input provided. This can make your code more flexible and adaptable.
  3. Readability: Overloading can make your code easier to read and understand, as you can use the same method or function name to perform different tasks, rather than using a different name for each variation.
  4. Type checking: Some programming languages use overloading as a way to perform type checking and ensure that the correct version of a method or function is called based on the data types of the arguments provided.

Overall, overloading can help you create more efficient, reusable, and readable code in your programs.

Java Overloading Example

Method Overloading:

Creating two or more methods in the same class with same name but different number of parameters or different types of parameters is known as method overloading. Let’s consider the following code segment which demonstrates method overloading:

In the above code segment, the method sum is overloaded. Java compiler decides which method to call based on the type of the parameters in the method call. For example, if the method is called as shown below:

The sum method with two integer parameters will be invoked and the output will be Sum of two integers is: 30. 

If the method is called as shown below:

The sum method with two float parameters will be invoked and the output will be Sum of two floats is: 2.7.

Note: It should be remembered that Java automatically performs type conversion from one type to another type. So, proper care should be taken while defining overloaded methods.

Constructor Overloading:

As constructor is a special type of method, constructor can also be overloaded. Several constructors can be defined in the same class given that the parameters vary in each constructor. As an example for constructor overloading, let’s consider the following code segment:

In the above code segment we can see that the constructor Sqaure() is overloaded. One constructor accepts a single integer parameter and returns a single square with side s. Another constructor accepts two integer parameters and returns n number of squares each with side s.

Note: In the above examples I have defined only two overloaded methods or constructors. You can create any number of overloaded methods or constructors based on your requirements.

Take your time to comment on this article.

Leave a Comment