What is meant by dynamic dispatch in Java, and why do we need it in the context of inheritance?
Dynamic method dispatch is the mechanism in which a call to an overridden method is resolved at run time instead of compile time. This is an important concept because of how Java implements run-time polymorphism.
In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented programming (OOP) languages and systems.
In Java, Dynamic method dispatch is a technique in which object refers to superclass but at runtime, the object is constructed for subclass. In other words, it is a technique in which a superclass reference variable refers to a subclass object.
Dynamic dispatch. It simply means that the Objective-C runtime decides at runtime which implementation of a particular method or function it needs to invoke.
What is meant by dynamic dispatch in 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 it in the context 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.
Without dynamic dispatch, virtual methods wouldn't make sense, which is central for abstraction and encapsulation.
Recommended reading: Wikipedia article on Dynamic Dispatch
Other Answers discus the theory, this is an example to show why dynamic dispatch (also called late binding) is needed.
Assume you have an Class 'Rectangle`.
public class Rectangle{
public void draw() {
System.out.println("___\n| |\n---");
//does it look nice?
}
}
And you have a subclass with rounded corners
public class RoundedRectangle extends Rectangle {
@Override
public void draw() {
System.out.println("assume it is a rectangle with rounded corners");
}
}
Now assume you have a method getting with an Parameter of Type Rectangle, which is used in the method to invoke the draw method
public class Demo() {
...
public demonstration(Rectangle rect) {
rect.draw();
}
...
}
It the argument of this method is of class Rectangle then it will draw
___
| |
---
But when the argument is of type RoundedRectangle, then we expect:
assume it is a rectangle with rounded corners
And this is where late binding is needed for: When the code compiles, it is not clear which method needs to be invoked by rect.draw(); I could be Rectangle.draw(), RoundedRectangle.draw(), or the draw method of any other yet not implemented subclass of Rectangle.
Summary (from the view of an developer):
So late binding means: invoke the method of given signature (Method name, and argument list) for an instance where is only known that it is of an specific class or subclass - but it is not known which class it is exactly. And when a method is overriden, then ivoke the overriding method.
So there is no Polymorphism vs. Late Binding -- It is you need Late Binding when you have Polymorphism. In Java and in C++ (and i belive, in every other Object Oriented language too)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With