Quick Enquiry Form

×

    EnquiryEnquire Now

    Quick Enquiry

      Method Overloading in Java

      method-overloading-in-java
      Blog

      Method Overloading in Java

      Introduction of Method Overloading in Java

      Method Overloading in JAVA is a feature that enables a class to have multiple methods with the same name, even if the argument lists for these individual methods are distinct from one another. It is very similar to the function “constructor overloading” feature in Java, which enables a class to have multiple constructors, each of which can take a different set of arguments.

      Why method overloading in Java?

      The readability of the program is improved when we only need to carry out a single operation, and doing so is accomplished by giving all of the methods the same name.

      In case, if the addition operation needs to be performed and you compose the method such as A(int a, int b) for two parameters and B(int c, int d, int e) for three parameters, then it may be challenging for you as well as other software developers to study the behavior of the method since its name varies. For example, suppose you have to perform addition on the given numbers, but there can be any number of arguments.

      Therefore, the method of overloading in JAVA is engaged in order to rapidly recognize the assigned program.

      Methods of Overloading in Java

      There are three different ways for method overloading in JAVA

      To be able to overload a method, the argument lists of the methods in question must be different in one of the following ways:

      1. The number of available parameters
      2. The data type of parameters
      3. Sequence of the data type of the parameters

      The Number of Available parameters

      As an illustration, this is an acceptable example of method overloading in Java

      add (int a, int b)

      add (int c, int d, int)

      In the above example, it is to be noted that the add() method is overloaded, by means of having the same names but with different arguments. The first add() method carries out the addition of two integers; whereas the second one carries out the same addition function but with three integers.

      Illustration

      class Adder{

      static int add(int u,int v){return u+v;}

      static int add(int x,int y,int z){return x+y+z;}

      }

      class TestOverloading1{

      public static void main(String[] args){

      System.out.println(Adder.add(21,24));

      System.out.println(Adder.add(15,17,19));

      }}

      OutPut

      45

      51

      The data type of parameters

      In this type, two methods are created that differ in their data types. One method is created with two integer arguments while the other includes the input of two double arguments which is similar to the below

      add (int, int)

      add (int, float)

      Illustration

      class Adder{

      static int add(int x, int y){return x+y;}

      static double add(double x, double y){return x+y;}

      }

      class TestOverloading2{

      public static void main(String[] args){

      System.out.println(Adder.add(21,24));

      System.out.println(Adder.add(17.2,18.3));

      }}

      Output

      45

      35.5

      Sequence of the Data type of parameters

      When the sequence of arguments in two methods is altered, method overloading in Java is performed.

      add (int, float)

      add (float, int)

      Illustration

      class DisplayOverloading3

      {

      public void disp(char a , int n)

      {

      System.out.println(“I am the first definition of method disp”);

      }

      public void disp(int n, char a)

      {

      System.out.println(“I am the second definition of method disp” );

      }

      }

      class Sample3

      {

      public static void main(String args[])

      {

      DisplayOverloading3 obj = new DisplayOverloading3();

      obj.disp(‘y’, 16 );

      obj.disp(17, ‘x’);

      }

      }

      Output:

      I’m the first definition of method disp

      I’m the second definition of method disp

      Case of method overloading that is invalid

      When discussing the argument list of a method, the method’s return type is not referred to. For instance, if two methods have similar names and parameters but have different return types, then this is considered to be an invalid example of method overloading in Java. This will result in an error during compilation.

      int add (int x, int y)

      float add (int x, int y)

      The use of different versions of the same method is an example of static polymorphism, which is applicable in method overloading in java.

      Things to Keep in Mind

      1. The phrases “compile-time binding” and “early binding” are essentially interchangeable terms for “static polymorphism.”
      1. The process of static binding takes place during the compilation process. An example of static binding is method overloading, which occurs when the binding of a method call to its definition takes place during the compilation process.

      Method Overloading and Promotion of Types

      Type promotion is the process by which a data type with a smaller size can be upgraded to a data type with a larger size. For instance, a byte data type can be upgraded to a short data type, and a short data type can be upgraded to an int, a long, or a double data type, amongst other possible promotions.

      What is its relationship to method overloading?

      The importance of type promotion cannot be overstated. If you don’t understand type promotion, you’ll think that the program will fail to compile, when in fact, the program will run perfectly because of type promotion.

      Type Promotion table:

      It is possible to elevate the data type on the left into any of the data types listed on the right side of the table.

      byte → short → int → long

      short → int → long

      int → long → float → double

      float → double

      long → float → double

      Some Valid/invalid Illustrations of method overloading in Java

      Illustration1:

      int mymethod(int x, int y)

      int mymethod(float a, float b)

      Result: It is suitable for method overloading in Java since the data types and arguments are different.

      Illustration 2:

      float mymethod(int u, float v)

      float mymethod(float x, int y)

      Result: The above illustration is valid for method overloading. The sequence of the first method is having (int, float) and the second method possesses (float, int), where the sequence of the data types parameters are altered.

      Illustration 3:

      int mymethod(int u, int v, float w)

      int mymethod(int x, int y, float var z)

      Result: Compile time error. Since the two methods are composed of a similar number of arguments, similar types of data, and are in the same sequence, the arguments are regarded as exactly the same and hence the compile-time error.

      Illustration 4:

      int mymethod(int x, int y)

      int mymethod(int z)

      Result: It is a Perfectly valid case of overloading, as the number of arguments varies.

      Illustration 5:

      int mymethod(int x, int y)

      float mymethod(int var, int var2)

      Result: Compile time error and invalid for method overloading since the data types, sequence, and the number of arguments is the same.

      Conclusion

      Thus, in this article, we have discussed the method overloading in java, its criteria, and conditions for valid and invalid execution of method overloading in Java.

      1
      Softlogic-Academy