Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Method Overloading in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 284
    Comment on it

    METHOD OVERLOADING:

    Having more than one methods with the same in the same scope. i.e. class is known as method overloading.

    It is useful in increasing the readability of the program. If a user wants to perform some operation say multiplication on two values and on three values, then we can give same name to the method just to avoid the confusion to the users. 

    Following conditions should be satisfied by the method for method overloading:

    • Number: Number of arguments passed to the method should be different.
    • Data type: Data typed of the different arguments passed in the method should be different.

    Example1:

    Number of arguments differ:

    class Multiply{  
      public void mulltiply(int a,int b){
      System.out.println(a*b);
       }  
      public void multiply(int a,int b,int c){
       System.out.println(a*b);
      }  
      
      public static void main(String args[]){  
      Multiply ob=new Multiply ();  
      ob.multiply(10,20); // Will  call the first method since it matches 2 arguments
      ob.multiply(10,20,30); // Will  call the second method since it matches 3 arguments
      
      }  
    }  

     

    Example2:

    Data type of the arguments differ:

    class Multiply{  
      public void mulltiply(float a,float b){
      System.out.println(a*b);
       }  
      public void multiply(int a,int b){
       System.out.println(a*b);
      }  
      
      public static void main(String args[]){  
      Multiply ob=new Multiply ();  
      ob.multiply(10,20); // Will  call the first method since it matches data type int.
      ob.multiply(0.5F,10.0F); // Will  call the second method since it matches data type float
      
      }  
    }  

    Method Overloading is one of the ways in which Java implements Polymorphism.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: