Wednesday, 10 May 2017

Simple Java Program to explain Methods Overloading

Basically, methods overloading is used to create methods that have the same name but different parameter lists and definitions. This technique is used when objects are required to perform similiar tasks but using different input parameters. When we call a method in an object Java matches up the method name first and then the number and type of the parameters to decide which one of definition is to execute. This process is known as Polymorphism

To create an overload method, we all have to do is to provide several different method definitions in the class, all with the same name but different in number and types of parameters. Simple java program is given below to explain method overloading. 

PROGRAM:

import java.util.Scanner;

class method_overloading
{
 
   int method(int l)
   {
          return l*l;
   }
   int method(int l,int b)
   {
      return l*b;
   }
   int method(int l,int b,int h)
   {
      return l*b*h;
   }

};
public class main_class
{
   public static void main(String args[])
   {
        Scanner in=new Scanner(System.in);

         method_overloading  m1=new method_overloading();
         int result;

         result=m1.method(20);
         System.out.print("\n\nArea of square is: "+result);

         result=m1.method(30,20);
         System.out.print("\n\nArea of Rectangle is: "+result);

         result=m1.method(20,30,40);
         System.out.print("\n\nVolume of the cuboidal is: "+result+"\n\n");

   }
}


OUTPUT:

 


EmoticonEmoticon