Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Inheritance. 1 Comment on Dynamic Method Dispatch
0
(0)

In this article we will look at dynamic method dispatch in Java which is a way to provide run-time polymorphism.

 

What is dynamic method dispatch?

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


Dynamic method dispatch is a mechanism which resolves the call to a overridden method at run-time based on the type of object being referred.

 

When is dynamic method dispatch possible?

 

It is possible only when the following are satisfied:

  • A class inherits from another class (inheritance)
  • Super class variable refers a sub class object
  • A overridden method is invoked using the super class reference

 

Why dynamic method dispatch?

 

Dynamic method dispatch is the way to provide run-time polymorphism in Java. Let’s look at what is static (compile-time) polymorphism and dynamic (run-time) polymorphism.

 

Before understanding what is the difference between static and dynamic polymorphism, let’s look at what is method binding. The process of associating or linking a method call with its definition (body) is known as method binding. This is can take place in two ways: at compile time or at run-time.

 

When method binding takes place at compile time it is known as static binding and if the method binding takes place at run-time it is known as dynamic binding. Static binding takes place when there is no inheritance or if there is inheritance and the method is specified as static or final or private, or if the type of reference and type of the object it is referring to are same. In all other cases it will be dynamic binding.

 

Now, if the call to a polymorphic method (overloaded or overridden) is bound at compile time, it is known as static polymorphism or else if the call to a polymorphic method takes place at run-time it is known as dynamic polymorphism.

 

Run-time polymorphism always depends on the type of sub class object that is being referred to by the super class reference. This type of binding is also known as late binding.

 

Now, let’s look at some code example.

 

Example 1:

class Shape
{
	void area()
	{
		System.out.println("Area of shape");
	}
}

class Circle extends Shape
{
	void area()
	{
		System.out.println("Area of circle");
	}
}

class Rectangle extends Shape
{
	void area()
	{
		System.out.println("Area of rectangle");
	}
}

public class Driver
{
	public static void main(String[] args) 
	{
		Shape s = new Shape();
		s.area();
		s = new Circle();
		s.area();
		s = new Rectangle();
		s.area();
	}
}

 

In the above example, the call to area() method at line 30 is an example for static polymorphism. However at lines 31 and 33 the super class (Shape) reference s has been assigned objects of Circle and Rectangle respectively. Now the subsequent calls to area() method at lines 32 and 34 are bound at run-time which are an example for run-time polymorphism. Output of the above program is:

 

Area of shape
Area of circle
Area of rectangle

 

So, we can say that run-time polymorphism is possible only when there is inheritance and dynamic method dispatch.

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

it awesome like you thank you

Leave a Reply

Your email address will not be published. Required fields are marked *