Pages

Monday 30 September 2013

Method Overloading and Dynamic Method Dispatch



Method Overriding:

                The method overriding is the process in which methods are implemented in subclasses with the same name and type as that of the method present in their superclass. 

Note: By method overriding Java implements runtime polymorphism (“one interface, multiple methods”).

               In the below program example method overriding is implemented. 

package blog.inheritance.Super1;

public class X
{
            int a;
            int b;
  
   X(int a,int b)
   {
               this.a=a;
               this.b=b;
   }
  
  void add()
  {
              System.out.println("am add method in class X");
              System.out.println("addition is "+(a+b));
  }
    
}

package blog.inheritance.Super1;

public class Y extends X
{
            int c;
            int d;
  
   Y(int a,int b,int c,int d)
   {
              super(a,b);
             
              this.c=c;
              this.d=c;
   }
  
   void add() // overridden method from class X
   {
              
               super.add();// call to superclass add method
               System.out.println();
               System.out.println("am add methos in class Y");
               System.out.println("addition reult "+(c+d));
   }
  
   public static void main(String[] args)
   {
              Y objY=new Y(20, 30, 40, 50);
             
              objY.add();
   }
  
}

Output:

add method in class X
addition is 50

am add methos in class Y
addition reult 80

Dynamic Method Dispatch: 

               It is the mechanism by which a call to an overridden method is resolved at runtime.
               When call is made to an overridden method, Java determines which version of the overridden method to be invoked based on the type of the object being referred at the time of call happens.

 package blog.inheritance.Super1;

public class X
{
            int a;
            int b;
  
   X(int a,int b)
   {
               this.a=a;
               this.b=b;
   }
  
  void add()
  {
              System.out.println("am add method in class X");
              System.out.println("addition is "+(a+b));
  }
    
}

package blog.inheritance.Super1;

public class Y extends X
{
            int c;
            int d;
  
   Y(int a,int b,int c,int d)
   {
              super(a,b);
             
              this.c=c;
              this.d=c;
   }
  
   void add() // overridden method from class X
   {
               System.out.println("am add methos in class Y");
               System.out.println("addition reult "+(c+d));
   }
  
   public static void main(String[] args)
   {
              Y objY=new Y(20, 30, 40, 50);
              X objX=objY; // reference of object Y is assigned to variable of type X.
             
              objX.add(); // variable of type X is calling the method add
   }
  
}
Output:

am add methos in class Y
addition reult 80

            In the above example program you can clear see that variable of type X which holds the reference to object Y is calling the add () method. The version of the add () method that is invoked is the version present in the class Y. The call to the overridden method is resolved at the runtime based on the reference present in the variable objX, which is of type X. The reference in the variable objX is pointing to the object Y. So in the above example program add() method present in the class Y is invoked.