What is Dynamic Method Dispatch?
Dynamic Method Dispatch is a process in which the call to an overridden method is resolved at runtime rather than at compile-time. When an overridden method is called by a reference, Java determines which version of that method to execute based on the type of object it refer to. This is also know as runtime polymorphism.
We will see this through an example.
- Dynamic Binding = Late binding
- Abstract classes cannot be instantiated, but they can be sub-classed (Base for a child class)
- An abstract method is a method that is declared without an implementation
- Abstract class may contain a mix of methods declared with or without an implementation
- When an abstract class is sub-classed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract
- Dynamic method dispatch is a mechanism by which a call to an overridden method is resolved at runtime. This is how java implements runtime polymorphism.
- Upcasting : Casting a subtype to a supertype, upward to the inheritance tree.
- Runtime Polymorphism = Dynamic Polymorphism