Dynamic Method Dispatch:

Here in method overriding, we observed that method overriding is a disadvantageous fact, because in this concept only sub class property is coming as output, super class property is hidden. So to eliminate this disadvantage we need run-time polymorphism which is also known as dynamic method dispatch.

  • Dynamic method dispatch is a mechanism in which a call to an overriden method is reserved at runtime rather than compile time.
  • This is a mechanism that represents how java implement runtime polymorphism.
  • In this mechanism a super class reference variable can refer to a sub class object.
  • When an overriden method is called through a super class reference, java determines which method is executed and this is happened at runtime.

wap to illustrate dynamic method dispatch

Example;

DynamicMethodDispatchDemo.java

 

package java8s;

 

class Example
{
      void print()
      {
            System.out.println("javacamp.in");
      }
}

class Example12 extends Example
{
      void print()
      {
            System.out.println("java8s.com");
      }
}

class Example123 extends Example
{
      void print()
      {
            System.out.println("javabytrilochan.blogspot.in");
      }
}
public class DynamicMethodDispatchDemo {

      public static void main(String[] args) {
           
            Example ref;
           
            Example ob1=new Example();
            Example12 ob2=new Example12();
            Example123 ob3=new Example123();
           
            ref=ob1;
            ref.print();
           
            ref=ob2;
            ref.print();
           
            ref=ob3;
            ref.print();

      }

}

 

Output

method dispatch
 

 

 

 

 

 

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment