Dynamic Method Dispatch in Java
Dynamic Method Dispatch in Java mechanism allows a subclass to provide a particular implementation of a method that is specified in its superclass. It means that depending on the type of object that is referenced through a superclass reference variable, Java selects which version of the method to execute at runtime. Dynamic Dispatch Method is the key concept of object-orientated programming is polymorphism, which makes it possible by Java's efficient Dynamic Method Dispatch. It allows a subclass to create its implementation and override a method from the superclass. The type of the object being referred to defines that this overrides at runtime. Dynamic Dispatch Method is also known as Runtime Polymorphism.
Syntax:
class One { // code } Class Two extends One { // code { class Three extends One { // code }
Filename: DynamicMethodDispatch.java
class Cycle // superclass { public void vehicle() //overrides super class "Cycle" { System.out.println("Cycle is a Two Wheeler"); } } class Bike extends Cycle // subclass { public void vehicle() //overrides super class "Cycle" { System.out.println("Bike is a Two Wheeler"); } } class Auto extends Cycle // subclass { public void vehicle() //overrides super class "Cycle" { System.out.println("Auto is a Three Wheeler"); } } class Car extends Cycle // subclass { public void vehicle() //overrides super class "Cycle" { System.out.println("Car is a Four Wheeler"); } } public class DynamicMethodDispatch { public static void main(String[] args) { Cycle c1 = new Bike(); // Dynamic Method Dispatch happens here c1.vehicle(); // Output will be "Bike is a Two Wheeler" Cycle c2 = new Auto(); // Dynamic Method Dispatch happens here c2.vehicle(); // Output will be "Auto is a Two Wheeler" Cycle c3 = new Car(); // Dynamic Method Dispatch happens here c3.vehicle(); // Output will be "Car is a Two Wheeler" } }
Output:
Explanation:
This program creates three subclasses: Bike class, Auto class, and Car class, along with the class Cycle. The vehicle() method declared in Cycle is overridden by the subclasses Bike, Auto, and Car. The DynamicMethodDispatch class declares objects of the types Cycle, Bike, Auto, and Car in the main() method.
After that, the program extends to handle all types of objects, using each one to invoke a vehicle().
The kind of object that is referred to at the time of the call determines the version of the vehicle() that is run.
Super Class:
A class that gets properties and behaviors from any other class, referred to as the subclass, is known as the superclass. It is also known as a base class or parent class.
Example:
class Cycle // superclass { public void vehicle() //overrides super class "Cycle" { System.out.println("Cycle is a Two Wheeler"); } }
Sub Class:
A class that receives properties and behavior from a superclass is known as a subclass. It can also replace inherited behaviors and properties with new classes that are specific to the subclass.
Example:
class Bike extends Cycle // subclass { public void vehicle() //overrides super class "Cycle" { System.out.println("Bike is a Two Wheeler"); } }
Advantages:
- Dynamic Method Dispatch When objects are a part of the same inheritance hierarchy, dispatch allows them to be treated similarly. It will increase the reusability and flexibility of the code.
- It allows you to develop code that is based on interfaces or superclasses instead of on specific implementations. It enables easy maintenance and increases the ability to adapt the code for modifications.
- Without changing the current code, we can create new subclasses that implement inherited methods in their town in a particular manner. It enables a codebase that is extendable and modular.
- Programming to superclasses or interfaces reduces the demand for specific implementations. It enables code maintenance and modification without interfering with other program components.
- We can write code that is compatible with a variety of objects that have a similar superclass or interface by using Dynamic Method Dispatch. It allows the reusability of code in various program components.
- Based on the actual type of the object, the runtime decides which method to call. It implies that we can use different objects at runtime to change the way the program behaves.
- It promotes developing code with a high level of abstraction. We can write clear code and make it easy to handle code by considering the main purpose of an object rather than its implementation while applying the program to interfaces or abstract classes.