Show Mobile Navigation

Webring

Powered by WebRing.

Concept of dynamic dispatch in Java

Sachin R Kukale - 02:05
Dynamic dispatch is the concept of storing the reference of child type into reference variable of parent type (class/interface).
In dynamic dispatch, if you are calling an instance method of child class with parent reference type and if that method is present in parent class then compiler will compile the code successfully. In this case the method will be either inherited or overridden in the child class.
If you are calling instance method of child class which is not present in Parent type by using the parent reference type then compiler will generate a compilation error.
If you want to access the child class instance method using the concept of dynamic dispatch then you are required to type cast the reference variable of parent type to child class reference variable.
You can implement the dynamic dispatch with classes/interfaces that do not have inheritance relationship. It is mandatory to have inheritance relationship between class/interface to apply the concept of dynamic dispatch.
Using dynamic dispatch you can not access the newly defined members i.e. members which are defined in child class but are not present in parent class.
Syntax:
=() ;
Ex:
class A{}
class B extends A{}
class Test{
public static void main(String args[]){
A a =new B();
B b=(B)a;
}

}

0 comments:

Post a Comment