You are here:   HomeJava ProgrammingInheritanceLocal Inner Classes in Java Example
by Dinesh Thakur Category: Inheritance

The process of defining methods with same name but with different functionalities is termed method overloading. For example, an overloaded draw() method can be used to draw anything, from a circle to an image. Methods with same name, namely, draw, but with different arguments can be used for all cases.

When a method in an object is called, Java verifies its name and argument type so that the appropriate method definition is executed.

To create an overloaded method, several different method definitions are created in the class with the same name but with different parameter lists (either in number or type of arguments). Java allows method overloading as long as each parameter list for the method in question is unique.

Java differentiates overloaded methods based on the number and type of parameters and not on the return type of the method. A compiler error would occur when two methods with the same name and same parameter list but different return types are created.

Program gives an example of an overloaded method.

 

class WorkerDetail

            {
                  int Code, Salary;
                 String Name;
                 void setData()
                      {
                         Code=11;
                         Name="Sunil Bansal";
                         Salary=13000;
                      }
                         void setData(int x, String y)
                              {
                                 Code=x;
                                 Name=y;
                                 Salary=6000;   
                              }
                                  void setData(int x, String y, int z)
                                   {
                                      Code=x;
                                      Name=y;
                                      Salary=z;
                                   }
                                      void ShowDetail()
                                            {
                                                System.out.println("Employee Code : "+Code);
                                                System.out.println("Employee Name : "+Name);
                                                System.out.println("Employee Salaryary : "+Salary);
                                            }
            };
                  class JavaMethodOverloading
                   {
                      public static void main(String args[])
                        {
                             WorkerDetail WD = new WorkerDetail();
                             WorkerDetail WD1 = new WorkerDetail();
                             WorkerDetail WD2 = new WorkerDetail();
                             WD.setData();
                             WD1.setData(12,"Amit Aggarwal");
                             WD2.setData(13, "Sunny Dadwal", 5000);
                             WD.ShowDetail();
                             WD1.ShowDetail();
                             WD2.ShowDetail();
                        }
                  };

Example of Method Overloading in Java



What's New and Popular