Dynamic Method Dispatch

Dynamic Method Dispatch is a mechanism in Java that allows the JVM to determine at runtime which method to call. It is a feature of runtime polymorphism, where the method to be executed is determined based on the object type, not the reference type. This enables more flexible and reusable code.

In dynamic method dispatch, a superclass reference can point to a subclass object, and when a method is invoked on the reference, the method from the subclass is executed, even though the reference is of the superclass type.

Example Using Software Engineer Hierarchy

We will create a SoftwareEngineer class hierarchy, demonstrating dynamic method dispatch.

  1. Superclass Employee: Represents general employees.
  2. Subclass SoftwareEngineer: Represents a software engineer.
  3. Subclass Manager: Represents a manager.

We will create objects of Employee, SoftwareEngineer, and Manager, but we will use references of type Employee to refer to these objects, demonstrating dynamic method dispatch when the method is called on the reference.

Program

// Superclass Employee
class Employee {
    String name;
    int employeeId;

    public Employee(String name, int employeeId) {
        this.name = name;
        this.employeeId = employeeId;
    }

    public void showDetails() {
        System.out.println("Employee Name: " + name);
        System.out.println("Employee ID: " + employeeId);
    }

    public void calculateSalary() {
        System.out.println("Base Salary: $50,000");
    }
}

// Subclass SoftwareEngineer (Mahesh)
class SoftwareEngineer extends Employee {
    String programmingLanguage;

    public SoftwareEngineer(String name, int employeeId, String programmingLanguage) {
        super(name, employeeId);
        this.programmingLanguage = programmingLanguage;
    }

    @Override
    public void showDetails() {
        super.showDetails();
        System.out.println("Role: Software Engineer");
        System.out.println("Programming Language: " + programmingLanguage);
    }

    @Override
    public void calculateSalary() {
        System.out.println("Salary: $120,000");
    }
}

// Subclass Manager (Paani)
class Manager extends Employee {
    int teamSize;

    public Manager(String name, int employeeId, int teamSize) {
        super(name, employeeId);
        this.teamSize = teamSize;
    }

    @Override
    public void showDetails() {
        super.showDetails();
        System.out.println("Role: Manager");
        System.out.println("Team Size: " + teamSize);
    }

    @Override
    public void calculateSalary() {
        System.out.println("Salary: $150,000");
    }
}

// Subclass Tester (Vineeth)
class Tester extends Employee {
    String testingTool;

    public Tester(String name, int employeeId, String testingTool) {
        super(name, employeeId);
        this.testingTool = testingTool;
    }

    @Override
    public void showDetails() {
        super.showDetails();
        System.out.println("Role: Tester");
        System.out.println("Testing Tool: " + testingTool);
    }

    @Override
    public void calculateSalary() {
        System.out.println("Salary: $90,000");
    }
}

// Main class to test dynamic method dispatch
public class Main {
    public static void main(String[] args) {
        // Employee reference for dynamic method dispatch
        Employee ref;

        // Mahesh - Software Engineer
        ref = new SoftwareEngineer("Mahesh", 101, "Java");
        System.out.println("=== Mahesh's Details ===");
        ref.showDetails();
        ref.calculateSalary();
        System.out.println();

        // Paani - Manager
        ref = new Manager("Paani", 102, 5);
        System.out.println("=== Paani's Details ===");
        ref.showDetails();
        ref.calculateSalary();
        System.out.println();

        // Vineeth - Tester
        ref = new Tester("Vineeth", 103, "Selenium");
        System.out.println("=== Vineeth's Details ===");
        ref.showDetails();
        ref.calculateSalary();
    }
}
/*
=== Mahesh's Details ===
Employee Name: Mahesh
Employee ID: 101
Role: Software Engineer
Programming Language: Java
Salary: $120,000

=== Paani's Details ===
Employee Name: Paani
Employee ID: 102
Role: Manager
Team Size: 5
Salary: $150,000

=== Vineeth's Details ===
Employee Name: Vineeth
Employee ID: 103
Role: Tester
Testing Tool: Selenium
Salary: $90,000
*/

Dynamic Method Dispatch is a powerful mechanism in Java that enables runtime polymorphism, allowing the JVM to determine the method implementation to be executed based on the actual object type, not the reference type. It leverages inheritance and method overriding, enabling flexible and reusable code structures.

Key Takeaways:

  1. Polymorphism Implementation: It provides a way to achieve polymorphism, where a single reference type (e.g., Employee) can point to multiple object types (SoftwareEngineer, Manager, Tester).
  2. Runtime Decision: The decision of which method implementation to call is made at runtime, ensuring that the most specific method (e.g., the overridden version) is invoked based on the actual object type.
  3. Extensibility and Maintainability: This mechanism allows the addition of new subclasses with specific method implementations without altering the base class or existing code, promoting scalability and maintainability.
  4. Dynamic Binding: It utilizes dynamic binding, ensuring that the appropriate overridden method is executed based on the actual object type, enhancing runtime flexibility.

Thus, Dynamic Method Dispatch is fundamental in designing adaptable systems, promoting code reusability, and supporting polymorphic behavior in Java applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top