Method Overloading Different Types

Method overloading occur with in a class or parent and child class. Method overloading not effected by the return type. 

Method overloading achieve by different method signatures

1- Different parameters 

  • The number of parameters should be different in two methods.
  • Different data types of the parameters of methods.
  • Different order of the parameters of methods.

 

In java programming we can override the method with different method signatures. i.e.. If the methods can have same name but with different parameters list like number/count of the parameters, sequence of the parameters, and data types of the parameters within the same class and parent and child class.

Why do we need Method Overloading ?

Number of times programmer think that questipon, why we need method overloading in java. I our project we need to do same kind of the operation like add numbers with different ways i.e.. for different inputs.

public void addNumber(int a, int b);

public void addNumber(int a, int b, int c);

public void addNumber(int a,int b,float c)

In the example described above, we are doing the addition operation for different inputs. It is very hard to find many different meaningful names for single action.

 

Method 1: By changing the number of parameters.

import java.io.*;  

class Addition{    

    // adding two integer values.
    public int add(int a, int b){          

        int sum = a+b;
        return sum;
    }      

    // adding three integer values.

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

        int sum = a+b+c;
        return sum;
    }    
}

  class Test{
    public static void main (String[] args) {         

        Addition ob = new Addition();         
        int sum1 = ob.add(1,2);
        System.out.println("sum of the two integer value :" + sum1);
        int sum2 = ob.add(1,2,3);
        System.out.println("sum of the three integer value :" + sum2);

          }
}


Output:

sum of the two integer value :3
sum of the three integer value :6

 

Method 2: By changing the Data types of the parameters

import java.io.*;  

class Addition{     

    // adding three integer values.
    public int add(int a, int b, int c){                                                             

        int sum = a+b+c;
        return sum;
    }     

    // adding three double values.
    public double add(double a, double b, double c){          

        double sum = a+b+c;
        return sum;
    }
}  

class Test {
    public static void main (String[] args) {         

        Addition ob = new Addition();        

        int sum2 = ob.add(1,2,3);

        System.out.println("sum of the three integer value :" + sum2);

        double sum3 = ob.add(1.0,2.0,3.0);

        System.out.println("sum of the three double value :" + sum3);

   }
}

 

Output:

sum of the three integer value :6
sum of the three double value :6.0

 

Method 3: By changing the Order of the parameters

import java.io.*;                                                                                    

  class OrderTest{     

    public void sequence(String name,  int id){         

        System.out.println("Name:"+ name +" "+"Id :"+ id);

    }

         public void sequence(int id, String name){         

        System.out.println("Id:"+ id+" "+"Name:"+ name);

    }
}

  class Test{
    public static void main (String[] args) {         

        OrderTest obj= new OrderTest();         

        obj.sequence("Ram", 10);

        obj.sequence(20,"Kisan");          

    }
}

 

Output:

Name:Ram Id :10
Id :20 Name:Kisan

Related Articles

post a comment