why Dynamic method dispatch?
I am just want to know what is dynamic method dispatch in case of java and why do we need that in case of inheritance(what is the need of DMD) and Which one is better Polymorphism in C++ or DMD in java .
What is dynamic method dispatch in case of Java
Think of "dispatch" as "determining which method to call".
The "dynamic" part simply says that it is determined at runtime. That is, which method is to be called is determined at runtime.
why do we need that in case of inheritance
Without inheritance / polymorphism we wouldn't need this. The type of an expression would be decidable at compile time, and which method that would have been called at runtime would be known when compiling the program.
With inheritance / polymorphism we don't know the concrete runtime type of an expression, thus which method to be called must be "dynamically" determined during runtime.
what is the need of DMD
Without it virtual methods wouldn't make sense, which is central for abstraction and encapsulation.
Which one is better Polymorphism in C++ or DMD in Java
I see little difference between how C++ and Java handles polymorphism / dynamic disptach. The main differences as I see it are
- C++ allows multiple inheritance which complicates the business slightly
- Java has only "pure" virtual methods while C++ also allows "non-pure" virtual methods.
Recommended reading: Wikipedia article on Dynamic Dispatch