Turn Desktop View Off
by Dinesh Thakur

Another important feature of the Java is polymorphism. A famous phrase 'one interface, multiple methods' or 'once instance, multiple forms' is for polymorphism. General classes of actions are defined, and according to the exact nature of situation the specific action is called. A set of related activities are designed in a generic interface, and same interface is used to specify a general class of action. To select the specific action is the responsibility of the compiler's job. Polymorphism allows you to create a sensible, readable, clean, and resilient code.

 

There are two following ways to implements polymorphism

 

  1. Overloading
  2. Run-time polymorphism

                        • Dynamic method dispatching                  

• Overridden

Overloading

In a class, if there are two methods having the same name but with different set of parameters, the methods are called overloaded method, and the concept is called overloading. Overloading is one of the mechanisms by which Java implements polymorphism.

 

In overloaded method, method names are same, but their type of parameters, number of parameters and their order are different. Return types doesn't matter that can be same or different. Whenever any overloaded method is called, then Java uses types and/or number of arguments to decide the exact version of the overloaded method to call, which parameters match with the argument used in the method call.

 

Example

public class OverloadingDemo
{
             public static void main(String arg[])
        {
                    sum (1, 2);
                    sum(1,2.5F);
                    sum (6.7F, 1.2);
                    sum(6.9F, 2.3F);
        }
             public static void sum(int a, int b)
        {
                    System.out.println("Sum = " + (a+b));
        }
             public static void sum(int a, float b)
        {
                    System.out.println("Sum = " + (a+b));
        }
             public static void sum(float a, double b)
        {
                   System.out.println("Sum = " + (a+b));
        }
             public static void sum(double a, double b)
        {
                   System.out.println("Sum = " + (a+b));
        }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

 

 

Sum = 3

 

Sum = 3.5

 

Sum = 7.899999809265137

 

Sum = 9.200000047683716

 

 

Yet, whenever an overload method is called, then Java search for a method of same types of parameters, as passed as argument in method call, but always it is not possible to get the exact match. In some cases Java automatically use the type conversion implicitly, that plays a major role in overloading.

 

In the given code, the method call sum(6.9F, 2.3F) passed two float argument, but there is no method of same type of parameters. Here, Java implicit type conversion plays an important role, it convert the float to double, and sum( double a, double b) method is called.

 

Overridden

 

In a class hierarchy, when there is a method in the subclass having the signature same as the signature of a method of its superclass, the subclass method is called override method of the superclass, this concept is called overriding. In overriding the superclass method will be hidden by the subclass overridden method. Overridden method supports the run-time polymorphism.

 

Example:

class Abc
{
                int i, j;
            Abc(int a, int b)
        {
                 i = a;
                 j = b;
         }
            void show ()
        {
                System.out.println("i :: " + i + " j :: " + j);
         }
}
class xyz extends Abc
{
                int k;
            xyz(int a, int b)
        {
                 super (a, b);
                 k = a+b;
        }
            void show ()
       {
            System.out.println("k ::"+ k);
        }
}
public class OverrideDemo
{
         public static void main(String arg[])
      {
              xyz ob = new xyz(1, 2);
              ob.show();
       }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

k :: 3

 

In the above code, when show() method is invoked by an object of class Xyz, the version of show() defined in Xyz class is called, that is overridden version of the method defined in Abc. If you want to call or access the superclass method then using super keyword you can call it.

class Abc
{
        int i, j;
        Abc(int a, int b)
     {
            i = a;
            j = b;
      }
        void show ()
      {
           System.out.println("i :: " + i + " j :: " + j);
       }
}
class xyz extends Abc
{
           int k;
       xyz(int a, int b)
    {
          super (a, b);
          k = a+b;
     }
        void show ()
     {
             super.show();
             System.out.println("k :: "+ k);
      }
}
        public class OverrideDemo
   {
           public static void main(String arg[])
         {
             xyz ob = new xyz(1, 2);
             ob.show();
          }
   }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output:

 

I :: 1 j::2

 

K :: 3

 

Why overridden method? Some time, generalize class define a method that will be common to all subclasses, but in subclasses it will have some different implementation with its (subclass) need, then the superclass method has to override. It's another way to implements the "one interface, multiple forms".

class Employee
{
             private int Empid;
             private String EmpName;
             public Employee(int eid, String ename)
        {
                 Empid = eid;
                 EmpName = ename;
        }
             public void display()
        {
                 System.out.println("Employee Name is: " + EmpName);
                 System.out.println("Employee ID is: " + Empid);
         }
}
class EmpAdhoc extends Employee
{
                 private int noofworkingdays;
                 private float dailywages;
                 private float netsalary;
              public EmpAdhoc(int eid, String ename, int workday, float wages)
       {
                    super (eid, ename);
                    noofworkingdays= workday;
                    dailywages=wages;
                    calSalary ();
       }
              public void calSalary()
       {
                    netsalary=noofworkingdays * dailywages;
       }
               public void display()
       {
                    super.display();
                    System.out.println("No. of working days :: " + noofworkingdays);
                    System.out.println("Daily wages :: " + dailywages);
                    System.out.println("Net Salary:: " + netsalary);
       }
}
          public class OverrideDemo
{
              public static void main(String arg[])
      {
                  EmpAdhoc ob = new EmpAdhoc(1001,"Alice",25, 600.00F);
                  ob.display() ;
      }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Example:

 

 

Output:

 

Employee Name is: Alice

Employee ID is: 1001

No. of working days :: 25

Daily wages :: 600.0

Net Salary :: 15000.0

 

In the above code the display() method is overridden, and in both classes it is showing the corresponding data members. So that, signatures are same, but implementation are different.

 

Dynamic Method Dispatching

 

In overriding, there is a major problem of calling the method of the superclass. Because the subclasses object call only their own member method. The only way to call the superclass method by using the super keyword in the subclass member method, or write another method which are responsible to call superclass method using super keyword. So that, to gives power to overriding, the dynamic method dispatching is another and very powerful mechanism to resolves overriding problem at run time. Dynamic method dispatching is important, and implements run-time polymorphism.

 

Example:

class Parent

{
              public void display()
       {
                   System.out.println("I am in class Parent");
        }
}
              class Childl extends Parent
        {
                    public void display()
               {
                       System.out.println("I am in class Childl");
               }
        }
                     class Child2 extends Parent
              {
                         public void display()
                    {
                             System.out.println("I am in class Child2");
                    }
              }
                     public class DynamicMethodDispatchDemo
                {
                          public static void main(String arg[])
                      {
                               Parent p = new Parent();
                               Childl cl = new Childl();
                               Child2 c2 = new Child2();
                               Parent ref;
                               ref = p;
                               ref.display() ;
                               ref = cl;
                               ref.display();
                               ref = c2;
                               ref.display();
                      }
                }

 

 

 

 

Output:

 

I am in class Parent

I am in class Child1

I am in class Child2