Dynamic method dispatch in java

In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism.

Dynamic Method Dispatch

Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method is to be called is decided at runtime according to the type of object it refers.Dynamic method dispatch is performed by JVM not compiler.
Dynamic method dispatch allows java to support overriding of methods and perform runtime polymorphism.It allows subclasses to have common methods and can redefine specific implementation for them.This lets the superclass reference respond differently to same method call depending on which object it is pointing.

When parent class reference variable refers to a child class object than its called upcasting and using this technique dynamic method dispatch perform.
We can not use derived class reference to refer to a base class object.
For example:

When you run above program, you will get below output.
Output:-

Area of rectangle: 32
Area of square: 64
Area of triangle: 16

That’s all about Dynamic method dispatch in java.

Was this post helpful?

Leave a Reply

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