Java Method Overloading with Examples

This article will discuss the concept of method overloading in the Java programming language. We will start by defining overloading, its purpose, benefits, and the difference between overloading and overriding. We will then look at the different ways of method overloading, their syntax, and a program to better understand how it works.

To grasp the concept more quickly and master it, I recommend practising each code in the article and making modifications and adjustments to cover different scenarios. This will help you understand the code better and learn how to apply it in different situations.

What is Method Overloading in Java?

Method overloading happens when you have different methods that have the same name but different input parameters and return parameters. For example, you may have one method called “Area(int a)”, and another method “Area(float x, floaty)”.

Both methods have the same name, but the first method has the variable ‘a’ which is of type integer as the parameter, and the second method has the variables “x”, and “y” of the type float as its parameters.

What is the need for Java method overloading?

Let me explain this using an example, say we need a method to calculate areas of a few shapes, depending on the shape, our method parameters will change. For example, if we have a square of side ‘a’, then our method will be “Area(int a)”. If we have a rectangle, our method will be “Area(int a, int b)”, and if we have a circle, our method will be “Area (int r)”.

You might have a doubt: why use the same name for the method? Why not just create different method names also? Well, in the future, other programmers or even you may get confused because the two methods have the same behavior but different names. It would be better to rename one of the methods to avoid confusion.

Therefore, method overloading is the better way to accomplish this task as it drastically increases the readability of the program.

What are the advantages of Java method overloading?

Method overloading allows for a more readable and reusable program by providing multiple ways to call the same method with different parameters. This can make code more concise and easier to understand, and it can also make code more reusable by allowing different parts of the program to use the same method with different parameters.

Method overloading is a programming technique that allows multiple methods to have the same name but different parameters. This can help to reduce the complexity of a program by making it easier to reuse code.

Method overloading allows programmers to perform a task more efficiently and effectively by providing multiple ways to call the same method with different parameters. Method overloading allows you to access methods that perform similar functions with slightly different parameters and data types.

What are the different types of method overloading in Java?

In Java, there are 3 types of method overloading, let us take a look at each one with the help of a program:

1. Overloading by changing the number of parameters

Method overloading can be achieved by modifying the number of input parameters of the method,

Here is an example illustrating the same:

class Firstcode
{
    public void area(int a)
    {
        int x = a * a;
        System.out.println("The area of the square is "+x);
    }
    public void area(int a, int b)
    {
        int x = a * b;
        System.out.println("The area of the rectangle is "+x);
    }
}
class main
{
    public static void main(String[] args) 
    {
        Firstcode FC = new Firstcode();
        FC.area (5);
        FC.area (5, 10);
    }
}

Output:

The area of the square is 25
The area of the rectangle is 50

In the example shown above, the first method has only one input parameter, and the second method has 2 input parameters, and both of the share the same name.

In the main function, when we call the method by passing 1 input parameter, then the method to calculate the area of the square is called, when calling the method by passing 2 input parameters, the method to calculate the area of the rectangle is invoked.

2. Overloading by changing the data type of the parameters

Methods can also be overloaded if they have the same name but different parameter types. This means that a method can have multiple definitions, each with a different set of parameters. The compiler will choose the correct definition based on the types of arguments passed to the method.

Here is an example:

class Firstcode
{
    public void area(int a)
    {
        int x = a * a;
        System.out.println("The area of the square is "+x);
    }
    public void area(double a)
    {
        double x = 3.14 * a * a;
        System.out.println("The area of the circle is "+x);
    }
}
class main
{
    public static void main(String[] args) 
    {
        Firstcode FC = new Firstcode();
        FC.area (5);
        FC.area (6.2);
    }
}

Output:

The area of the square is 25
The Area of the circle is 120.7016

In the above example, both the methods have the same name and the same number of parameters, however, the first method has an input parameter of integer datatype, and the second one has a parameter of double datatype.

Therefore, in the main function, when the method is called by passing an integer argument, the method to calculate the area of a square was invoked, and when passed with an argument of the double datatype, the method to calculate the area of a circle was invoked.

3. Overloading by changing the order of the parameters

Method overloading can also be obtained by rearranging the parameters of two or more overloaded methods.

Here is an example:

class FirstCodeEmployee 
{
    public void EMP_ID(String emp_name, int emp_id)
    {
            System.out.println("Name :" + emp_name+"\tId :"+emp_id);
    }
    public void EMP_ID(int emp_id, String emp_name)
    {
        System.out.println("Id :"+emp_id+"\t\tName :"+emp_name);
    }
}
class main {
  
    public static void main(String[] args)
    {
        FirstCodeEmployee emp1 = new FirstCodeEmployee();
        System.out.println("Employee Details:");
        emp1.EMP_ID("Gopi Kiran", 21213);
        emp1.EMP_ID(21023, "Anusha PA");
    }
}

Output:

Employee Details:
Name: Gopi Kiran Id: 21213
Id: 21023 Name: Anusha PA

Some FAQs about Java Method Overloading

Before concluding this article, let us take a look at some of the most commonly asked questions and doubts regarding method overloading:

Q1. Can static methods be overloaded?

A. Yes, we can. Two or more static methods can have the same name but different parameters.

Q2. Can the main function in Java be overloaded?

A. We already know that the main function is a static method, and just like all static methods, the main function can also be overloaded.

Q3. Can the methods that differ only by static keywords be overloaded?

A. Nope! In Java, 2 methods cannot be overloaded if they differ only by static keyword and the types of parameters and the number of parameters are the same.

Q4. Can we perform operator overloading in Java?

A. Java, unlike C++, does not allow users to define overloaded operators. However, Java does overload operators internally, for example, the + operator is overloaded for string concatenation.

Q5. Can methods be overloaded based on the return type?

A. It is not possible to overload a function by its return type. This is the same behavior in C++.

Q6. What happens when the exact prototype does not match with arguments?

A. When the exact prototype does not match with arguments, the compiler takes 2 steps. First, it converts the type to a higher one (in terms of range) within the same family.

Second, it performs type conversion to the next higher family. For example, if there is no long data type available for an int data type, it will search for the float data type.

Q7. What are the differences between overriding and overloading?

Overloading Overriding
Example of compile-time polymorphism. It is an example of run-time polymorphism
Overloading helps in increasing the readability of the program. Grants specific methods that are provided by its parent class
the return type can or can not be the same return type must be exactly the same
Method overloading can be done to private, and final methods can be Method overriding can’t be done to Private and final methods
The argument list should be different  The Argument list should be exactly the same 

Conclusion

You have now learned quite a lot about method overloading in Java as we have covered topics like the definition it, the need for overloading, its advantages, and the different types of method overloading with the help of example codes. We also glimpsed through some of the most frequently asked questions and answers related to method overloading.

Leave a Reply

Your email address will not be published. Required fields are marked *