Method Overloading in Java

Good day, readers We’ll talk about method overloading in java in today’s topic. Before writing a Java program, it is essential to understand these crucial ideas. If you don’t know, we also covered Java methods, decision-making, and loop control before these. Now, hurry up! In the past, we have also provided a brief overview of a number of Java-related topics, including Operator in Java, Data Types in Java, Difference between C++ & Java, and many others. This information is very beneficial for your understanding. So without further ado, let’s get to the point:

Method Overloading in JAVA

Method overloading is the practice of having multiple methods with the same name but various parameter values. Compile-time polymorphism is connected to overloading.

Polymorphism is a type of OOP which involves method overloading. Due to polymorphism, things or processes can behave differently depending on the context in which they are used. Method overloading is one example of how methods respond to the types and quantities of their arguments.

Java supports polymorphism in part through the use of method overloading. The idea of polymorphism in object-oriented programming deals with different forms. Later on, we’ll discuss polymorphism in more detail.

Method overloading has the benefit of making code easier to read and maintain. Overloaded methods are advised to have similarities in how they operate even though it is possible to have methods having the same name that carry out entirely different functions.

Method overloading can be done in two different ways.

  1. Different argument datatypes
  2. Various number of arguments

Different Argument Datatypes

In this instance, we have developed two methods with various data types. The first add method accepts two arguments of type integer, and the second add method accepts two arguments of type double. After that, each returns its own results.

// Example of Method Overloading in Java by GeektoCode
class Add{
   int add(int a, int b){
       System.out.println("The addition of two integer i.e. "+a+" and "+b+" is "+(a+b));
       return 0;
   }
   double add(double a, double b){
       System.out.println("The addition of two double i.e. "+a+" and "+b+" is "+(a+b));
       return 0;
   }
}
public class GeektoCode {
    public static void main(String args[]){
        Add obj = new Add();
        obj.add(15,20);
        obj.add(17.3,28.9);
    }
}

OUTPUT:-

The addition of two integers i.e. 15 and 20 is 35
The addition of two double i.e. 17.3 and 28.9 is 46.2

Various Number of Arguments

In this example, we’ve written two methods: the first add() method adds two numbers, and the second add method adds three numbers.

// Example of Method Overloading in Java by GeektoCode
class Add{
    int add(int a, int b){
         System.out.println("The addition of "+a+" and "+b+" is "+(a+b));
         return 0;
    }
    int add(int a, int b, int c){
         System.out.println("The addition of "+a+" and "+b+" and "+c+" is "+(a+b+c));
         return 0;
    }
}
public class GeektoCode {
    public static void main(String args[]){
         Add obj = new Add();
         obj.add(15,20);
         obj.add(17,28,35);
    }
}

OUTPUT-

The addition of 15 and 20 is 35
The addition of 17 and 28 and 35 is 80

Significant Points

  • Within a single class, multiple methods may share the same name if they take different arguments. Method overloading is the name of this feature.
  • Increasing the number of arguments or changing the type of arguments are two ways to achieve method overloading.
  • If we only modify the return type of methods, it is not method overloading. There must be variations in the quantity of parameters.
  • In the same way that methods can be overloaded in Java, so can constructors.
  • Keep in mind that different methods can share the same name as long as their parameter counts and/or types are distinct.

Thus, Java’s method of overloading is the main topic here. I hope you find this useful. Wait for this to meet in the next post, where we will discuss a brand-new aspect of Java. Thanks a lot.

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry