Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Dynamic Method Dispatch in Java Example

In Java, Dynamic method dispatch is a technique in which object refers to superclass but at runtime, the object is constructed for subclass.

i.e. It is a technique in which a superclass Reference Variable refers to a subclass object. It is also known as Upcasting in java.

Java uses this mechanism to resolve a call to an overridden method at runtime rather than compile time. The overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.

In the dynamic dispatch method technique, we can declare a super class reference variable equal to sub class object like this:
      SuperClass sc = new ChildClass(); // sc is super class reference variable which is pointing to child class object. 

Dynamic dispatch method is important because it is a process through which Java implements runtime polymorphism. Let’s understand the dynamic method dispatch more clearly by different example programs.

Java Dynamic Method Dispatch Example Programs


Program source code 1:
Let’s create a Java program to illustrate Dynamic Method Dispatch using hierarchical inheritance. 

package dynamicMethodDispatchProgram; 
public class A 
{ 
  void m1() 
  { 
    System.out.println("m1-A"); 
  } 
} 
public class B extends A 
{ 
// Overriding m1() method. 
  void m1() 
  { 
    System.out.println("m1-B"); 
  } 
} 
public class C extends B 
{ 
// Overriding m1() method. 
  void m1() 
  { 
    System.out.println("m1-C"); 
  } 
} 
public class Test 
{ 
  public static void main(String[] args) 
  { 
     A a = new A(); // Object of type A. That is "a" is a reference variable of type A which is pointing to object of class A. 
      a.m1(); 

     B b = new B(); // Object of type B. "b" is a reference variable of type B which is pointing to object of class B. 
      b.m1(); 

     C c = new C(); // Object of type C. "c" is a reference variable of type C which is pointing to object of class C. 
      c.m1(); 
  
     A a2; // A reference variable of type A. 
     a2 = b; // Reference a1 refers to Class B's object. 
     a2.m1(); 
     a2 = c; // Reference a1 refers to Class C's object. 
     a2.m1(); 
     B b2 = c; // Reference variable b2 of type B refers to class C's object. 
     b2.m1(); 
   } 
}
Output: 
       m1-A 
       m1-B 
       m1-C 
       m1-B 
       m1-C 
       m1-C

Explanation:
In the preceding program, we have created one super class called A and it’s two sub classes B and C. These sub classes override m1( ) method.


1. Inside the main() method in class Test, initially, objects of type A, B, and C are declared.
a. When a.m1(); will be executed, it will call method m1() of class A because reference variable “a” is pointing towards object of class A.b.

b.m1(); will call m1() of class B because reference variable “b” refers to object of class B.
c. c.m1(); will invoke m1() of class C because reference variable “c” is pointing towards object of class C.

2. When A a2 is declared, initially it will point to null.
a. a2.m1(); will call m1() of class B because “a2” is reference variable of type A but it is pointing towards class B’s object.

b. a2.m1(); will call m1() of class C. This is because “a2” is referring to class C’s object.
3. b2.m1(); will call m1() of class C because “b2” of type B is pointing to class C’s object. 

If you have any problem to understand the above concept then first, I will recommend to you for going below the topic. In this topic, we have explained in detail the working concept of Java compiler and JVM which you can understand easily.
Method overriding in Java | Example ProgramsThis is an example of dynamic method dispatch. Let’s see next example program.
Program source code 2:

package dynamicMethodDispatchProgram; 
public class X 
{ 
 X() 
 { 
    System.out.println("Parent class constructor"); 
      m1(); 
  } 
void m1()
{ 
   System.out.println("Parent method"); 
 } 
} 
public class Y extends X 
{ 
  Y() 
  { 
     System.out.println("Child class constructor"); 
  } 
void m1() 
{ 
  System.out.println("Child class method"); 
 } 
} 
public class Demo extends Y 
{ 
  public static void main(String[] args) 
  { 
    Y y = new Y(); 
     y.m1(); 
    X x = new Y(); 
     x.m1(); 
    Y y2 = new Demo(); 
     y2.m1(); 
   } 
}
Output: 
       Parent class constructor 
       Child class method 
       Child class constructor 
       Child class method
 
       Parent class constructor 
       Child class method 
       Child class constructor 
       Child class method 
       Parent class constructor 
       Child class method 
       Child class constructor 
       Child class method

Explanation of flow of execution of the above program:

1. Inside the main() method of Demo class, when the statement A a = new A(); will be executed, the control of execution will be immediately transferred class Y’s constructor. 

From there, super Class Constructor will be called and the control of execution will be transferred to class X’s constructor. The first output will be printed “Parent class constructor”.

2. From the super class constructor, m1() method of class Y will be called because the reference variable “y” is pointing to the object of class Y. The second output will be “Child class method”.

3. After the execution of m1() method, the control of execution will be again immediately transferred to class Y’s constructor. The third output will be “Child class constructor”.

4. y.m1(); will call m1() of class Y because “y” refers to object of class Y. The fourth output is “Child class method”.
Similarly, for the other two statements.

Let’s take another example program to understand concept of dynamic method dispatch in java.

Program source code 3:

package dynamicMethodDispatchProgram; 
public class XX 
{ 
// Declaration of instance block. 
   { 
     m1(); 
   } 
XX() 
{ 
   System.out.println("Parent class constructor"); 
    m1(); 
} 
void m1() 
{ 
  System.out.println("Parent class method"); 
 } 
} 
public class YY extends XX 
{ 
  YY() 
  { 
     System.out.println("Child class constructor"); 
  } 
void m1()
{ 
   System.out.println("Child class method"); 
 } 
} 
public class Myclass 
{ 
  public static void main(String[] args) 
  { 
    XX x = new YY(); 
    x.m1(); 
  } 
}
Output: 
       Child class method 
       Parent class constructor 
       Child class method 
       Child class constructor 
       Child class method

Explanation:
1. During the execution of above program, when the statement XX x=new YY(); will be executed, the constructor of class YY will be called and the control of execution will be immediately transferred to class YY’s constructor. From there, super class constructor XX() will be called.

But we know that before the execution of constructor, instance block is executed. Inside the instance block, m1() of class YY will be called because the reference variable “x” is pointing child class object. Therefore, the first output is “Child class method”.

2. After the complete execution of instance block, super class constructor is executed. Therefore, the second output is “Parent class constructor”.


3. Inside the super class constructor, m1(); will call m1() method of class YY. Therefore, the third output is “Child class method”.

4. After the complete execution of superclass constructor, the control of execution will be transferred back to child class constructor. It will print “Child class constructor”. This is the fourth output.

5. x.m1(); will call m1() of class YY because the reference variable “y” is pointing to the object of class YY. Therefore, the fifth output is “Child class method”.

Let’s create a program where we will declare two method m1() and m2() in the parent class but in the child class, we will override only m1() method. We will also declare new method m3() in the child class. Let’s see the following source code.

Program source code 4:

package dynamicMethodDispatchProgram; 
public class ParentClass 
{ 
 void m1()
 { 
    System.out.println("ParentClass m1"); 
  } 
void m2() 
{ 
   System.out.println("ParentClass m2"); 
 } 
} 
public class ChildClass extends ParentClass 
{ 
  void m1() 
  { 
     System.out.println("ChildClass m1"); 
  } 
void m3() 
{ 
  System.out.println("ChildClass m3"); 
 } 
} 
public class Mytest 
{ 
 public static void main(String[] args) 
 { 
   ParentClass p = new ChildClass(); 
    p.m1(); 
    p.m2(); 
 // p.m3(); // The method m3() is undefined for the type ParentClass. 
  } 
}
Output: 
       ChildClass m1 
       ParentClass m2

Key points: 
In the dynamic method dispatch mechanism, there are two key points that you have to keep in mind.
1. The object can call the overriding method of child class as well as all non-overridden methods of the superclass. For example, in the preceding program, using reference variable “p”, we are called m1() and m2() methods.

2. The object cannot call methods which are newly added in the child class. For example, we have declared a new method m3() in the child class but it cannot be called because the method is undefined for type ParentClass.

Final words
Hope that this tutorial has covered almost all important points related to dynamic method dispatch technique in Java with realtime example programs. I hope that you will have understood this topic and enjoyed it.
Thanks for reading!!!
Next ⇒ Abstraction in Java⇐ PrevNext ⇒

The post Dynamic Method Dispatch in Java Example appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Dynamic Method Dispatch in Java Example

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×