METHOD OVERLOADING

Method Overloading


To develop java programs sun people gave some standards as follows:
  • In order to avoid naming collision for the method identifiers sun people came up with new concept i.e. “method overloading”.
  • Defining more than one method with the same name but different signatures with in the same class is called as “Method Overloading”.

Syntax for Method Overloading :

 
                        
                    class A
                    {
                       void m1()
                       {
                          ---------;
                       }
                       void m1(int i)
                       {
                          ---------;
                       }
                       void m1(int I,float f)
                       {
                          ---------;
                       }
                    }     
                        

Method Signature Rules :

  • Name of the method.
  • List of the method Arguments.
  • Type of the method Arguments.
  • Order of the method Arguments.

Example 1:

 
						
                    class Forest
                    {
                       void plants()
                       {
                          System.out.println("forest is empty");
                       }
                       void plants(int i)
                       {
                          System.out.println("Forest has integer type plants");
                       }
                       int plants(float f)
                       {
                          System.out.println("forest has int with Float type plants.");
                          return 20;
                       }
                       double plants(long l)
                       {
                          System.out.println("forest has long type plants ");
                          return 20.55f;
                       }
                    } 
                        
Note
  • Method return type is not a part of the method signature it means based on the return type two methods can’t be equal.

Example 2:

 
						
                     class Forest
                     {
                        void plants()
                        {
                           System.out.println("forest is empty");
                        }
                        int plants(int i)
                        {
                           System.out.println("Forest has integer type plants");
                           return i;
                        }
                        int plants(float f)
                        {
                           System.out.println("forest has int with Float type plants.");
                           return 20;
                        }
                        double plants(long l)
                        {
                           System.out.println("forest has long type plants ");
                           return 20.55f;
                        }
                        void plants(double d)
                        {
                           System.out.println("forest has double type plants.");
                        }
                     }
                     public class Environment 
                     {
                        public static void main(String[] args)
                        {
                           Forest f=new Forest();
                           f.plants();
                           f.plants(20l);
                           f.plants(10.35f);
                           f.plants(50);
                           f.plants(15.25);     
                         }
                     }                       	

   Output:

forest is empty forest has long type plants forest has int with Float type plants. Forest has integer type plants forest has double type plants.';

Example 3:

 
						
                     class Forest
                     {
                        void plants()
                        {
                           System.out.println("forest is empty");
                        }
                        int plants(int i)
                        {
                           System.out.println("Forest has integer type plants");
                           return i;
                        }
                        int plants(float f)
                        {
                           System.out.println("forest has int with Float type plants.");
                           return 20;
                        }
                        double plants(long l)
                        {
                           System.out.println("forest has long type plants ");
                           return 20.55f;
                        }
                        void plants(double d)
                        {
                           System.out.println("forest has double type plants.");
                        }
                     }
                     public class Environment
                     {
                        public static void main(String[] args)
                        {
                           Forest f=new Forest();
                           f.plants();
                           System.out.println(f.plants(20l));
                           System.out.println(f.plants(10.35f));
                           System.out.println(f.plants(50));
                           f.plants(15.25);
                        }
                     }                        
                       	

   Output:

forest is empty forest has long type plants 20.549999237060547 forest has int with Float type plants. 20 Forest has integer type plants 50 forest has double type plants.

Example 4:

 
						
                     class Forest
                     {
                        void plants()
                        {
                           System.out.println("forest is empty");
                        }
                        void plants(int i)
                        {
                           System.out.println("Forest has void type plants:"+i);
                        }
                        int plants(double d)
                        {
                           System.out.println("forest has int with integer type plants."+d);
                           return 20;
                        }
                        double plants(int i,double d)
                        {
                           System.out.println("forest has int & double type plants: "+i);
                           return d;
                        }
                        double plants(String name)
                        {
                           System.out.println("forest has double with string type plants:"+name);
                           return plants(20,30.25);
                        }
                        int plants(float f)
                        {
                           return 44;
                        }
                     }
                     public class Environment
                     {
                        public static void main(String[] args)
                        {
                           Forest f=new Forest();
                           f.plants();
                           f.plants(20);
                           f.plants(30.25);
                           f.plants(10,40.25);
                           f.plants("tamrind");
                           f.plants(22.2f);
                        }
                     }                        
                       	

   Output:

forest is empty Forest has void type plants:20 forest has int with integer type plants.30.25 forest has int & double type plants: 10 forest has double with string type plants:tamrind forest has int & double type plants: 20
Note
  • Parameter values may or may not be used.
  • We can use parameter also as a return value.

Example 5:

 
						
                     public class Vehicle
                     {
                        int i;
                        void bike()
                        {
                           System.out.println("2 wheeler");
                        }
                        void bike(int k)
                        {
                           i=k+20;
                        }
                        float bike(float f)
                        {
                           float f1;
                           f1=f+20.25f;
                           return 10.77f;
                        }
                        /* int bike(int l) */
                        /* {  */
                        /*   i=l+30;  */
                        /*   return 100;  */
                        /* }  */
                        
                        /* Here compilation fails  because of method name and signature type */
                        /*   & order is same for two methods but return type is different. */
                        /* Method return type is not a part of the method signature. */
                        void car()
                        {
                           System.out.println("4 wheeler"); 
                        }
                        Vehicle car(double d)
                        {
                           System.out.println("hi speed car"); 
                           return null; 
                        }
                        void car(int i,float f)
                        {
                           System.out.println("int-float type car");   
                        }
                        void car(float f,int i)
                        {
                           System.out.println("float-int type car");
                        }   
                        int car(double d,long l)
                        {
                           System.out.println("double-long type cars");
                           return 40;   
                        }
                        /* void car(int j,float k)  */
                        /* {  */
                        /* System.out.println("int-float type cars");  */
                        /* }  */
                     
                        /* Here compilation fails because the two methods have  */
                        /* same method name and signature type & order.  */
                        public static void main(String[] args) 
                        {
                           Vehicle v=new Vehicle();
                           v.bike();
                           v.bike(25);
                           v.bike(30.20f);
                           v.car();
                           v.car(50.55);
                           v.car(10,20.20f);
                           v.car(30.30f,50);
                           v.car(25.20,40);
                        }
                     }                        
                       	

   Output:

2 wheeler int type bike float type bike 4 wheeler hi speed car int-float type car float-int type car double-long type cars