Java Method Overloading

Java Method Overloading

Java Method Overloading


Method overloading is a concept in java programming in which we have same method for different underlying operations. Java method overloading is the part of static binding or compile time polymorphism. In this tutorial, we will discuss about method overloading, some important things to consider and ways to implement method overloading in our program.

Method Overloading

In method overloading, we create multiple methods with the same name but arguments and data type must be different. Method overloading increases the readability of the code and easy to remember.

Java Method Overloading
Java Method Overloading



Some Important things to remember about method overloading

  1. In method overloading, we can create multiple methods with the same name but parameters should be different in type and numbers.
  2. Method overloading happens in the same class only.
  3. In method overloading, return type can be different. 
  4. Method overloading provides you flexibility to call a similar method for different underlying operations.
  5. You can overload constructors as well for initializing instance variables with different values.
  6. If you will try to create a method with same name, number of argument and data types. Compiler will throw an error.

Let’s understand method overloading

A task is assigned to you in which you need to create a separate Math class. But you need to create this class as per below check list.

  1. You need to create a method which can add any number like int, float, double etc.
  2. You need to provide same name for all underlying operations.
  3. Return type of the method will depend upon the input data type, if we are adding two floats then return type may be float etc.
  4. Types of the argument may vary, we can add int with float, float with int, int with double etc.


So let’s create overloaded methods for Maths class.

class Maths {
 
   public int add(int a, int b) {
     return (a+b);
   }
 
   public float add(float a, float b) {
     return (a+b);
   }
 
   public double add(double a, double b) {
     return (a+b);
   }
 
   public double add(int a, double b) {
     return (a+b);
   }

   public static void main(String arg[]) {

     Maths m = new Maths();
     
     System.out.println(m.add(2, 4)); // 6
     System.out.println(m.add(2, 4.8)); // 6.8
     System.out.println(m.add(2.45, 4.8)); // 7.25
     System.out.println(m.add(2.45, 4)); // 6.45

   }
}

Look at the example above which implementing method overloading perfectly. So let’s discuss each and every aspect here. 

  1. You need to create a method which can add any number like int, float, double etc.

    We have created a class Maths with method add which can add different numbers.
  2. You need to provide same name for all underlying operations.

    Name of the method is same for different underline operations like add().
  3. Return type of the method will depend upon the input data type, if we are adding two floats then return type may be float etc.

    In method overloading return type is not considered, so return type can be different for different operations.
  4. Types of the argument may vary, we can add int with float, float with int, int with double etc.  

    As you can see in above example, we have add method with different types of arguments like we are adding int with float, int with double etc.

    But check this call statement in main method. We did not define any method which is adding floating point number with integer number. So how below statement is working and giving us the result.
    System.out.println(m.add(2.45, 4));
    The reason is type promotion. In type promotion, integer value can fit into double variable. So what’s happening here is, compiler first look for the method by matching the type of the arguments. It checks, do we have method which accepts it’s first argument as double and second as integer. Compiler failed to find such method, then compiler performs type promotion. Compiler checks, do we have any method where input values can fit into, then it finds such method add(double, double).

    In above statement, first input is double and second input is integer. Double can fit into double variable and integer value can also fit into double variable. So the output will be 6.45.

Check the type promotion chart below

Type Promotion in Java
Type Promotion in Java

Leave a Reply

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