Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

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

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext