I have seen some resources where it state that dynamic dispatch and the late binding are the same. If so then binding should be equal to dispatching. In some places they state overloading/early binding/ static dispatch as same and overriding/late binding/ dynamic dispatch as same.
So I came up with an analogy to understand this. Is the below analogy correct ? or how can I modify the below explanations.
We have a class structure as below.
class Board {
public void show(){};
}
class Shape{
public void draw(Board board) {};
}
class Square extends Shape {
public void draw(Board board) {
System.out.println("Drawing a Square");
};
}
class Circle extends Shape {
public void draw(Board board) {
System.out.println("Drawing a Circle");
};
}
We have :
Shape shape = createShape(type); // This will return any of Shape, Square, Circle
shape.draw(board);
We have :
Board myBoard = new Board();
myBoard.show();
And I came up with few explanations,
Binding : Deciding the actual type for the
shape(can be Shape, Square or Circle). Given that if type ofshapeis known only at the run time it islate binding. Deciding type ofmyBoardcan be done in compile time. Which isearly bindingDispatching : Deciding the actual implementation for
drawis considereddispatching. Given that if the actual implementation ofdrawcan only be decided at run time it isdynamic dispatchingotherwise if it can be decided at compile time it is calledstatic dispatchingStatic Dispatch : Happens when I know at compile time which function body will be executed when I call a method. So
myBoard.show(), here the methodshowcan be statically dispatched. Whereshape.draw(board)we can't dispatchdrawstatically since we can't guarantee which function body will be executed at runtime.Single Dispatch (Dynamic) : An implementation for the
drawwill be chosen based only onshape's type, disregarding the type or value ofboard.Multiple Dispatch (Dynamic) : The types of the
shapeandboardtogether determine whichdrawoperation will be performed. (In this case it isDouble Dispatch)
Few resources I used :
- https://lukasatkinson.de/2016/dynamic-vs-static-dispatch/
- https://softwareengineering.stackexchange.com/questions/200115/what-is-early-and-late-binding/200123#200123
- https://en.wikipedia.org/wiki/Late_binding
- https://en.wikipedia.org/wiki/Dynamic_dispatch
- http://net-informations.com/faq/oops/binding.htm
- What is the difference between Binding and Dispatching in Java?