by Dinesh Thakur

In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it certainly refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.

class WorkerDetail

          {
              int c,s;
              String n;
              float h;
              void SetSalary(int x, String y, int z)
                   {
                      c=x;
                      n=y;
                      s=x;
                   }
                      void ShowDetail()
                        {
                            System.out.println("Code : "+ c);
                            System.out.println("Name : "+n);
                            System.out.println("Salary : "+s);
                        }
                            void getHra()
                              {
                                 h=(float)s*60/100;
                                 System.out.println("HRA : "+h);
                              }
          }
                 class officerDetail extends WorkerDetail
                    {
                       float h,g;
                       void getHra()
                          {
                             h=(float)s*75/100;
                             System.out.println("HRA : "+h);
                          }
                             void getGross()
                                  {
                                      g=s+h;
                                      System.out.println("Gross : "+g);
                                  }
                   };
                       class ExMethodOverloading
                                 {
                                     public static void main(String args[])
                                          {
                                              WorkerDetail wD = new WorkerDetail();
                                              officerDetail oD = new officerDetail();
                                              wD.SetSalary(121,"Aman",13000);
                                              oD.SetSalary(111,"Amrik",30000);
                                              System.out.println("Detail of Worker is :");
                                              wD.ShowDetail();
                                              wD.getHra();
                                              System.out.println("Detail of Officer is :");
                                              oD.ShowDetail();
                                              oD.getHra();
                                              oD.getGross();
                                          }
                                 };

Example of Method Overloading