Method Overloading in Java

Posted on

 Technique overloading

If two or extra technique in a category have identical title however completely different parameters, it is called technique overloading. Overloading at all times happen in the identical class.

The primary benefit of technique overloading is that it will increase the readability of a program.

Technique overloading is finished in 3 ways

  1. Differing the Variety of parameters.
  2. Differing the Knowledge kind of parameters
  3. Differing the Sequence of Knowledge kind of parameters.

Differing the Variety of parameters.

On this instance, we have now created two strategies, first add() technique performs addition of two numbers and second add technique performs addition of three numbers.

class Calci

           int add(int a,int b)

          {

               return a+b;

          } 

          int add(int a,int b,int c)

         {

          return a+b+c;

          } 

 public static void most important(String[] args){ 

    Calci c=new Calci ( );

     System.out.println(c.add(11,11)); 

     System.out.println(c.add(11,11,11)); 

    }

}

————————————————————————————————————————————— 

Output : 22

                 33

—————————————————————————————————————————————-

 Differing the Knowledge kind of parameters

On this instance, we have now created two strategies, first add() technique performs addition of two int numbers and second add technique performs addition of 1 int and one double numbers.

public class Caci

{

       int add(int a,int b)

                        {

                        return a+b;

                        } 

                        double add(int a,double b)

                        {

                        return a+b;

                  } 

            public static void most important(String[] args){ 

                        Calci c=new Calci ( );

                        System.out.println(c.add(11,11)); 

                        System.out.println(c.add(11,11.5)); 

                        }

            }

————————————————————————————————————————————— 

Output : 22

                  22.5

—————————————————————————————————————————————-

 Differing the Sequence of Knowledge kind of parameters

 On this instance, we have now created two strategies,having completely different sequence of datatype of parameters first add() technique performs addition of a int quantity and a double quantity and second add technique performs addition of a int and a double quantity.

e.g.: 

public class Caci

{

       double add(int a,double b)

                        {

                        return a+b;

                        } 

                        double add(double a,int b)

                        {

                        return a+b;

                  } 

            public static void most important(String[] args){ 

                        Calci c=new Calci ( );

                        System.out.println(c.add(11,11.5)); 

                        System.out.println(c.add(11.5,11)); 

                        }

            }

————————————————————————————————————————————— 

Output : 22.5

                 22.5

—————————————————————————————————————————————-

  • In overloading technique decision is at all times takes care by compiler on reference kind therefore overloading can be thought-about as compile time polymorphism, static polymorphism or early binding.
  • In java, technique overloading shouldn’t be doable by altering the return kind of the tactic solely due to ambiguity. And we achieve this then it is going to throw compile time error saying “Compile Time Error: technique XXX is already outlined in school X”.